(笔记)基本复合型数据类型的声明

--(1)使用%type定义变量 --

declare
n_name users.usedname%type;
begin
commit;
end;

 
--(2)定义记录类型变量 --

declare
--定义了名为myrecord的记录类型,该记录类型由整数型的myrecordnumber和日期型的mycurrentdate基本类型变量组成
type myrecord is record(
 myrecordnumber int,
 mycurrentdate date);
  --然后声明一个myrecord类型的变量;
 srecord myrecord;
 begin
 select * into srecord from testtable where recordnumber=68;
 dbms_output.put_line(srecord.mycurrentdate);
 end; 

 
--在PL/SQL程序中,select语句总是和into配合使用,into子句后面就是要《被赋值的变量》

 

--(3) 使用%rowtype定义变量 --

declare
  --声明
 mytable testtable%rowtype;
 begin 
 select * into mytable
 from testtable
 where recordnumber=88;
 dbms_output.put_line(mytable.currentdate);
 end; 

   
 --比较两者定义的不同:
--变量名 数据表.列名%type      //定义普通变量,类型和某列一致
--变量名 数据表%rowtype        //定义复合类型变量,类型与表结构相同

 

--(4) 定义一维表类型变量 (相当于高级语言中的一维数组)

declare
type tabletype1 is table of varchar2(4) index by binary_integer;
type tabletype2 is table of testtable.recordnumber%type index by binary_integer;
table1 tabletype1;
table2 tabletype2;
begin
table1(1):='大学';
table1(2):='大专';
table2(1):=88;
table2(2):=55;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;

 

 

--(5)定义多维表类型变量 (相当于多维数组)

declare
type tabletype1 is table of users%rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1(20) from users where user_id=29;
dbms_output.put_line(table1(20).usedname||table1(20).real_name);
end;

 

 

--(6)定义多维表类型变量--

declare
type tabletype1 is table of varchar2(20) index by binary_integer;
table1 tabletype1;
begin
table1(1):='北京';
table1(2):='上海';
table1(3):='天津';
dbms_output.put_line('总记录'||to_char(table1.count));
dbms_output.put_line('第一条记录'||table1.first);
dbms_output.put_line('最后一条记录'||table1.last);
dbms_output.put_line('第二条的前一条记录'||table1.prior(2));
dbms_output.put_line('第二条的后一条记录'||table1.next(2));
if (table1.exists(2)) then dbms_output.put_line('第二条记录存在');
else dbms_output.put_line('第二条记录不存在');
end if;
table1.delete(2);
dbms_output.put_line('==================');
dbms_output.put_line('总记录'||to_char(table1.count));
dbms_output.put_line('第一条记录'||table1.first);
dbms_output.put_line('最后一条记录'||table1.last);
dbms_output.put_line('第二条的前一条记录'||table1.prior(2));
dbms_output.put_line('第二条的后一条记录'||table1.next(2));
if (table1.exists(2)) then dbms_output.put_line('第二条记录存在');
else dbms_output.put_line('第二条记录不存在');
end if;
end;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值