总结:一个变量只能获取一个值,但是如果想在where条件中使用多个值时可以定义一个集合。
create table t (id number);
insert into t values(1);
insert into t values(2);
commit;
create table t1 (id number,name varchar2(200));
insert into t1 values(1,'hh');
insert into t1 values(2,'h2');
insert into t1 values(3,'h3');
create table t2( name varchar2(200));
--- 一个l_cnt变量只能存储一个值。
declare
l_cnt number;
begin
select count(*) into l_cnt from t;
insert into t2 select
name from t1 where t1.id =(select l_cnt from dual);
commit;
end;
/
NAME
----------
h2
---有多个值的时候,需要使用游标+集合
declare
type l_cnt_tab_type is table of varchar2(100);
l_cnt_tab l_cnt_tab_type;
l_batch_size pls_integer := 3000;
cursor cur_t is
select id from t;
begin
open cur_t;
loop
fetch cur_t bulk collect
into l_cnt_tab limit l_batch_size;
forall i in 1 .. l_cnt_tab.count
insert into t2
select name from t1 where t1.id in (l_cnt_tab(i));
commit;
exit when cur_t%notfound;
end loop;
close cur_t;
end;
/
匿名块where条件使用变量(游标+集合)测试
最新推荐文章于 2022-10-19 12:53:36 发布