8.4比较集合
- declare
- type name_table_type is table of varchar2(10);
- name_table name_table_type;
- begin
- if name_table is empty then
- dbms_output.put_line('name_table未初始化');
- end if;
- end;
- declare
- type name_table_type is table of varchar2(10);
- name_table1 name_table_type;
- name_table2 name_table_type;
- begin
- name_table1:=name_table_type('scott');
- name_table2:=name_table_type('smith');
- if name_table1=name_table2 then
- dbms_output.put_line('两个嵌套表完全相同');
- else
- dbms_output.put_line('两个嵌套表数值不同');
- end if;
- end;
- declare
- type nt_table_type is table of number;
- nt1 nt_table_typent_table_type:=nt_table_type(1,2,3,1);
- begin
- dbms_output.put_line('元素个数:'||cardinality(nt1));
- end;
- declare
- type nt_table_type is table of number;
- nt1 nt_table_typent_table_type:=nt_table_type(1,2,3);
- nt2 nt_table_typent_table_type:=nt_table_type(1,2,3,4);
- begin
- if nt1 submultiset of nt2 then
- dbms_output.put_line('nt1是nt2的子集);
- end if;
- end;
- declare
- type nt_table_type is table of number;
- nt1 nt_table_typent_table_type:=nt_table_type(1,2,3,5);
- v1 number:=&v1;
- begin
- if v1 member of nt1 then
- dbms_output.put_line('v1是nt1的元素');
- end if;
- end;
- declare
- type nt_table_type is table of number;
- nt1 nt_table_typent_table_type:=nt_table_type(1,2,3,5);
- begin
- if nt1 is a set then
- dbms_output.put_line('嵌套表NT1无重复值');
- end if;
- end;
- declare
- type id_table_type is table of number(6) index by binary_integer;
- type name_table_type is table of varchar2(10) index by binary_integer;
- id_table id_table_type;
- name_table name_table_type;
- start_time number(10);
- end_time number(10);
- begin
- for i in 1..5000 loop
- id_table(i):=i;
- name_table(i):='name'||to_char(i);
- end loop;
- start_time:=dbms_utility.get_time;
- for i in 1..id_table.count loop
- insert into demo values(id_table(i),name_table(i));
- end loop;
- end_time:=dbms_utility.get_time;
- dbms_output.put_line('总计时间(秒):'||to_char((end_time-start_time)/100));
- end;
- declare
- type id_table_type is table of number(6) index by binary_integer;
- type name_table_type is table of varchar2(10) index by binary_integer;
- id_table id_table_type;
- name_table name_table_type;
- start_time number(10);
- end_time number(10);
- begin
- for i in 1..5000 loop
- id_table(i):=i;
- name_table(i);='name'||to_char(i);
- end loop;
- start_time:=dbms_utility.get_time;
- forall i in 1..id_table.count
- insert into demo values(id_table(i),name_table(i));
- end_time:=dbms_utility.get_time;
- dbms_output.put_line('总计时间(秒):'||to_char((end_time-start_time)/100));
- end;
- forall index in lower_bound..upper_bound sql_statement;
- forall index in indices of collection [between lower_bond.and.upper_bound] sql_statement;
- forall index in values of index_collection sql_statement;
- declare
- type id_table_type is table of number(6) index by binary_integer;
- type name_table_type is table of varchar2(10) index by binary_integer;
- id_table id_table_type;
- name_table name_table_type;
- begin
- for i in 1..10 loop
- id_table(i):=i;
- name_table(i):='name'||to_char(i);
- end loop;
- forall i in 1..id_table.count
- insert into demo values(id_table(i),name_table(i));
- end;
- declare
- type id_table_type is table of number(6) index by binary_integer;
- type name_table_type is table of varchar2(10) index by binary_integer;
- id_table id_table_type;
- name_table name_table_type;
- begin
- for i in 1..5 loop
- id_table(i):=i;
- name_table(i):='n'||to_char(i);
- end loop;
- forall i in 1..id_table.count
- update demo set name=name_table(i) where id=id_table(i);
- end;
- declare
- type id_table_type is table of number(6) index by binary_integer;
- id_table id_table_type;
- begin
- for i in 1..3 loop
- id_table(i):=i;
- end loop;
- forall i in 1..id_table.count
- delete from demo where id=id_table(i);
- end;
- declare
- type id_table_type is table of number(6) index by binary_integer;
- id_table id_table_type;
- begin
- for i in 1..10 loop
- id_table(i):=11-i;
- end loop;
- forall i in 8..10
- insert into demo (id) values (id_table(i));
- end;
- declare
- type id_table_type is table of number(6);
- id_table id_table_type;
- begin
- id_table:=id_table_type(1,null,3,null,5);
- forall i in indices of id_table
- delete from demo where id=id_table(i);
- end;
- create table new_demo as select * from demo where 1=0
- declare
- type id_table_type is table of demp.id%type;
- type name_table_type is table of demo.name%type;
- id_table id_table_type;
- name_table name_table_type;
- type index_pointer_type is table of pls_integer;
- index_pointer index_pointer_type;
- begin
- select * bulk collect into id_table,name_table from demo;
- index_pointer:=index_pointer_type(6,8,10);
- forall i in values of index_pointer
- insert into new_demo values(id_table(i),name_table(i));
- end;
- declare
- type dno_table_type is table of number(3);
- dno_table dno_table_typedno_table_type:=dno_table_type(10,20);
- begin
- forall i in 1..dno_table.count
- update emp set salsal=sal*1.1 where deptno=dno_table(i);
- dbms_output.put_line('第2个元素更新的行数:'||sql%bulk_rowcount(2));
- end;
- declare
- type emp_table_type is table of emp%rowtype index by binary_integer;
- emp_table emp_table_type;
- begin
- select * bulk collect into emp_table from emp where deptno=&no;
- for i in 1..emp_table.count loop
- dbms_output.put_line('雇员姓名:'||emp_table(i).ename);
- end loop;
- end;
- declare
- type ename_table_type is table of emp.ename%type;
- ename_table ename_table_type;
- begin
- delete from emp where deptno=&no;
- returning ename bulk collect into ename_table;
- dbms_output.put('雇员名');
- for i in 1..ename_table.count loop
- dbms_output.put(ename_table(i)|| ' ');
- end loop;
- dbms_output.new_line;
- end;