--(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;