The use of oracle table type variables

Reprinted in: http://www.itxuexiwang.com/a/shujukujishu/oracle/2016/0216/89.html?1455872314
Only one row of data can be saved using record type variables, which limits the number of rows returned by the SELECT statement​, if SELECT statement returns more than one row will be wrong. Oracle provides another custom type, the table type, which is an extension to the record type, allowing the processing of multiple rows of data, similar to a table.
The syntax for creating a table type is as follows:
TYPE table_name IS TABLE OF data_type [ NOT NULL ]
INDEX BY BINARY_INTEGER ; The
syntax is as follows:
--table_name The name of the table type created.
--IS TABLE indicates that the table type is created.
--data_type can be any valid PL/SQL data type, such as varchar2.
--INDEX BY BINARY_INTEGER Specifies that the system creates a primary key index that refers to a specific row in a table-type variable.
Example of using table type:
Example 1: Assign table type variable directly
declare
type my_emp is table of scott.emp%rowtype
index by binary_integer;
new_emp my_emp;
begin
new_emp(1).empno:=6800;
new_emp(1).ename:='tracy';
new_emp(1).job:='clerk';
new_emp(1).sal:=2500;
new_emp(2).empno:=6900;
new_emp(2).ename:='luck';
new_emp(2).job:='manager';
new_emp(2).sal:=4000;
dbms_output.put_line(new_emp(1).empno ||','||new_emp(1).ename||','||new_emp(1).job||
','||new_emp(1).sal);
dbms_output.put_line(new_emp(2).empno ||','||new_emp(2).ename||','||new_emp(2).job||
','||new_emp(2).sal);
end;
/
例二:使用表类型变量的方法:变量名.方法名[索引号]
declare
type my_emp is table of scott.emp%rowtype
index by binary_integer;
new_emp my_emp;
begin
new_emp(1).empno:=6800;
new_emp(1).ename:='tracy';
new_emp(1).job:='clerk';
new_emp(1).sal:=2500;
new_emp(2).empno:=6900;
new_emp(2).ename:='luck';
new_emp(2).job:='manager';
new_emp(2).sal:=4000;
dbms_output.put_line(new_emp.first||','||new_emp.count||','||new_emp.last);
dbms_output.put_line(new_emp(1).empno ||','||new_emp(1).ename||','||new_emp(1).job||
','||new_emp(1).sal);
dbms_output.put_line(new_emp(2).empno ||','||new_emp(2).ename||','||new_emp(2).job||
','||new_emp(2).sal);
--new_emp.delete(1);
dbms_output.put_line(new_emp.next(1));
end; declare Example 3: Combined with cursor #p#Paging title#e#
//


type my_emp is table of scott.emp%rowtype
index by binary_integer;
new_emp my_emp;
v_num number:=0;
cursor cur_emp is select empno,ename,job,sal from scott.emp;
begin
for v_emp in cur_emp loop
v_num:=v_num+ 1;
select * into new_emp(v_num) from scott.emp
where ename=v_emp.ename;
end loop;
for i in 1..new_emp.count loop
dbms_output.put_line(new_emp(i).empno ||','|| new_emp(i).ename||','||new_emp(i).job||
','||new_emp(i).sal);
end loop;
end;
/
Note: You cannot directly assign values ​​to table variables: select * into new_emp from scott.emp where deptno=30; This assignment method is wrong, the assignment needs to use subscripts, as in the above example.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327078462&siteId=291194637