Oracle使用手册(一)---声明变量

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif--建表
InBlock.gifcreate table student(
InBlock.gifrecordid number(38),
InBlock.gifsid int not null ,
InBlock.gifsname varchar2(50), 
InBlock.gifsdate date,
InBlock.gifsage  number(3)
InBlock.gif);
ExpandedBlockEnd.gif
*/

None.gif
-- 删除表
None.gif--
drop table student;
None.gif

None.gif
None.gif
-- 插入数据
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifset serveroutput on  --允许服务器输出
InBlock.gifdeclare 
InBlock.gifmaxrecords constant int:=100;
InBlock.gifi int:=1;
InBlock.gifbegin
InBlock.giffor i in 1..maxrecords 
InBlock.gifloop
InBlock.gifinsert into student(sid,sdate)values(i,sysdate);
InBlock.gifend loop
InBlock.gif--dbms_output.put_line('成功录入数据!');
InBlock.gifcommit;
InBlock.gifend;
ExpandedBlockEnd.gif
*/

None.gif
-- select * from student;
None.gif--
声明一个变量
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifdeclare 
InBlock.gifpi constant number(9):=3.1415926;
InBlock.gifbegin
InBlock.gifcommit;
InBlock.gifend;
ExpandedBlockEnd.gif
*/

None.gif
-- 复合数据类型(常见的五种)
None.gif--
1 .使用 %type 定义变量
None.gif--
为了让PL/SQL中变量的类型和数据表中的字段的数据类型一致,Oracle 9i提供了%type定义方法。
None.gif--
这样当数据表的字段类型修改后,PL/SQL程序中相应变量的类型也自动修改.
ExpandedBlockStart.gifContractedBlock.gif
  /**/ /*
InBlock.gif Declare
InBlock.gif        mydate student.sdate%type;
InBlock.gif    begin
InBlock.gif        commit;
InBlock.gif    end;
ExpandedBlockEnd.gif 
*/

None.gif
-- 2. 定义记录类型变量
None.gif--
将多个基本数据类型捆绑在一起的记录数据类型。
None.gif

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifset serveroutput on
InBlock.gif    declare
InBlock.gif        type myrecord is record(
InBlock.gif           sid int,
InBlock.gif           sdate date);
InBlock.gif        srecord myrecord; --声明一个自定义记录类型变量的实例
InBlock.gif    begin
InBlock.gif        select sid,sdate into srecord from student where sid=68;
InBlock.gif        dbms_output.put_line('ID: '|| srecord.sid ||'Date:'||  srecord.sdate); --'||': 它是字符串连接符.
InBlock.gif    end;
ExpandedBlockEnd.gif
*/

None.gif    
None.gif
-- 3.使用 %rowtype 变量
None.gif--
使用%type可以使变量获得字段的数据类型,使用%rowtype可以使变量获得整个记录的数据类型。
None.gif--
比较两者定义的不同:变量名 数据表.列名%type,变量名 数据表%rowtype。
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifset serveroutput on
InBlock.gifDeclare
InBlock.gif        mytableRow student%rowtype;
InBlock.gif    begin
InBlock.gif       select * into mytableRow
InBlock.gif       from student
InBlock.gif       where sid=88;
InBlock.gif       dbms_output.put_line(mytableRow.sid || mytableRow.sdate);
InBlock.gif    end;
ExpandedBlockEnd.gif
*/

None.gif
-- 4.定义一维表类型变量
None.gif--
表类型变量和数据表是有区别的,定义表类型变量的语法如下:
None.gif--
 ―――――――――――――――――――――――――――――――――――――
None.gif--
    type 表类型 is table of 类型 index by binary_integer;
None.gif--
    表变量名 表类型;
None.gif--
 ―――――――――――――――――――――――――――――――――――――
None.gif--
 类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
None.gif--
 这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
ExpandedBlockStart.gifContractedBlock.gif
  /**/ /*
InBlock.gif  Declare
InBlock.gif       type tabletype1 is table of varchar2(4) index by binary_integer; --定义一个字符型的一维数组
InBlock.gif       type tabletype2 is table of student.sid%type index by binary_integer;--定义了一个整数数型的数组
InBlock.gif    table1 tabletype1;  --实例声明
InBlock.gif    table2 tabletype2;  --实例声明
InBlock.gif    begin
InBlock.gif       table1(1):='学生';
InBlock.gif       table1(2):='职员';
InBlock.gif       table2(1):=88;
InBlock.gif       table2(2):=89;
InBlock.gif       dbms_output.put_line(table1(1)||table2(1));
InBlock.gif       dbms_output.put_line(table1(2)||table2(2));
InBlock.gif    end;
ExpandedBlockEnd.gif
*/

None.gif
-- 5.定义多维类型表变量
None.gif--
相当于定义多维数组.
None.gif--
注意在运行下面的语句前要在数据库中插入数据.
None.gif
Declare
None.gif      type tabletype1 
is   table   of  student % rowtype  index   by  binary_integer;
None.gif      table1 tabletype1;
None.gif    
begin
None.gif       
select   *   into  table1( 60 )
None.gif       
from  student
None.gif       
where  sid = 60 ;
None.gif       dbms_output.put_line(table1(
60 ).sid  || table1( 60 ).sdate);
None.gif    
end ;
None.gif
None.gif
None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值