复合变量
复合变量可以将不同的数据类型的多个值存储在一个单元中.由于复合数据类型可以有用户根据需要定义其结构,所以复合数据类型也称为自定义数据类型。
PL/SQL提供了两种类型的复合数据类型:
记录类型
在记录类型中可以存储多个标量值,与数据库中的行相似
记录表类型
允许用户在程序代码中使用”表”,以便存储多个行的数据。它只在程序运行期间有效。类似于程序代码中集合|数组。
记录类型:
必须使用TYPE语句定义记录类型的语法声明如下:
TYPE record_name is record(
Field_name data_type[not null, :=default value]
………
);
与%Rowtype不同之处是
1、%rowtype查询的是全部数据
2、记录类型必须使用type语法格式声明
//案例
SQL> declare
2 type empinfo is record( //声明一个记录类型
3 empno number(4),
4 ename varchar2(10),
5 job varchar2(9)
6 );
7
8 emp_data empinfo; //声明一个记录类型的变量
9 begin
10 select empno,ename,job into emp_data from emp where empno=7369; //查询的结果赋值给定义的变量
11 dbms_output.put_line(emp_data.empno||'/'||emp_data.ename||'/'||emp_data.job);
12 end;
13 /
记录表类型
它可以处理多个记录或多个行记录。
为什么使用记录表类型呢?
因为我们查询的数据的往往需要返回多行记录,所以需要记录表类型。
定义记录表类型
TYPE table_name is table of data_type[not null]
Index by binary_integer;//主键的索引
declare
type table_emp is table of emp%rowtype //创建一个表 此表的类型与emp表的类型一致
index by binary_integer;
type table_text is table of varchar2(20) //创建一个表 此表具有一个varchar2列的简单表
index by binary_integer;
empt table_emp; //声明记录表类型的变量
tabtext table_text;
begin
案例:
SQL> declare
2 type table_emp is table of emp%rowtype
3 index by binary_integer;
4 empt table_emp;
5 begin
6 empt(1).ename:='wangyi';
7 dbms_output.put_line(empt(1).ename);
8 end;
9 /
//返回总记录
SQL> declare
2 type table_emp is table of emp%rowtype
3 index by binary_integer;
4 empt table_emp;
5 begin
6 dbms_output.put_line(empt.count);
7 end;
8 /
0 //没有记录
//删除的操作
表名.Delete(记录数);
//检索记录变量
First:获取第一个的索引
Next:下一个的索引 但是必须有参数
Last:最后一个的索引
SQL> declare
2 type table_emp is table of emp%rowtype
3 index by binary_integer;
4 empt table_emp;
5 i number(2):=1;
6 begin
7 while i<10
8 loop
9 empt(i).ename:='wangyi';
10 i:=i+1;
11 end loop;
12
13 dbms_output.put_line(empt.count);
14
15 empt.delete(2);
16
17 dbms_output.put_line(empt.count);
18
19 dbms_output.put_line(empt.first);
20 dbms_output.put_line(empt.next(2));
21 dbms_output.put_line(empt.last);
22 end;
23 /
9
8
1
3
9
复合变量可以将不同的数据类型的多个值存储在一个单元中.由于复合数据类型可以有用户根据需要定义其结构,所以复合数据类型也称为自定义数据类型。
PL/SQL提供了两种类型的复合数据类型:
记录类型
在记录类型中可以存储多个标量值,与数据库中的行相似
记录表类型
允许用户在程序代码中使用”表”,以便存储多个行的数据。它只在程序运行期间有效。类似于程序代码中集合|数组。
记录类型:
必须使用TYPE语句定义记录类型的语法声明如下:
TYPE record_name is record(
Field_name data_type[not null, :=default value]
………
);
与%Rowtype不同之处是
1、%rowtype查询的是全部数据
2、记录类型必须使用type语法格式声明
//案例
SQL> declare
2 type empinfo is record( //声明一个记录类型
3 empno number(4),
4 ename varchar2(10),
5 job varchar2(9)
6 );
7
8 emp_data empinfo; //声明一个记录类型的变量
9 begin
10 select empno,ename,job into emp_data from emp where empno=7369; //查询的结果赋值给定义的变量
11 dbms_output.put_line(emp_data.empno||'/'||emp_data.ename||'/'||emp_data.job);
12 end;
13 /
记录表类型
它可以处理多个记录或多个行记录。
为什么使用记录表类型呢?
因为我们查询的数据的往往需要返回多行记录,所以需要记录表类型。
定义记录表类型
TYPE table_name is table of data_type[not null]
Index by binary_integer;//主键的索引
declare
type table_emp is table of emp%rowtype //创建一个表 此表的类型与emp表的类型一致
index by binary_integer;
type table_text is table of varchar2(20) //创建一个表 此表具有一个varchar2列的简单表
index by binary_integer;
empt table_emp; //声明记录表类型的变量
tabtext table_text;
begin
案例:
SQL> declare
2 type table_emp is table of emp%rowtype
3 index by binary_integer;
4 empt table_emp;
5 begin
6 empt(1).ename:='wangyi';
7 dbms_output.put_line(empt(1).ename);
8 end;
9 /
//返回总记录
SQL> declare
2 type table_emp is table of emp%rowtype
3 index by binary_integer;
4 empt table_emp;
5 begin
6 dbms_output.put_line(empt.count);
7 end;
8 /
0 //没有记录
//删除的操作
表名.Delete(记录数);
//检索记录变量
First:获取第一个的索引
Next:下一个的索引 但是必须有参数
Last:最后一个的索引
SQL> declare
2 type table_emp is table of emp%rowtype
3 index by binary_integer;
4 empt table_emp;
5 i number(2):=1;
6 begin
7 while i<10
8 loop
9 empt(i).ename:='wangyi';
10 i:=i+1;
11 end loop;
12
13 dbms_output.put_line(empt.count);
14
15 empt.delete(2);
16
17 dbms_output.put_line(empt.count);
18
19 dbms_output.put_line(empt.first);
20 dbms_output.put_line(empt.next(2));
21 dbms_output.put_line(empt.last);
22 end;
23 /
9
8
1
3
9