create table T ( c1 number, c2 number );
Table created.
declare
type r is record(
x number,
y number);
type rt is table of r;
d rt;
begin
select rownum, rownum bulk collect
into d
from all_Objects
where rownum <= 20;
forall i in 1 .. 20
insert into T values d (i);
end;
/
PL/SQL procedure successfully completed.
Но, представим, что таблица имеет три столбца и мы хотим добавить в третий столбец число 10 при вставке. Мы получим проблемы при попытке сделать это в лоб:
SQL> declare
2 type r is record (
3 x number,
4 y number );
5
6 type rt is table of r;
7
8 d rt;
9
10 begin
11 select rownum, rownum
12 bulk collect into d
13 from all_Objects
14 where rownum <= 20;
15
16 forall i in 1 .. 20
17 insert into T values ( d(i).x, d(i).y, 10);
18
19 end;
20 /
insert into T values ( d(i).x, d(i).y, 10);
*
ERROR at line 17:
ORA-06550: line 17, column 28:
PLS-00436: implementation restriction: cannot reference .... etc
Вместо этого мы можем использовать объекты и потом применить к ним SQL. Все что нам нужно - это объекты, которые будут имитировать PL/SQL типы:create or replace type r is object ( x number, y number );
/
Type created.
create or replace type rt is table of r;
/
Type created.
declare
d rt; -- this is now pointing to a database definition not a plsql definition
begin
select r(rownum, rownum) bulk collect
into d
from all_Objects
where rownum <= 20;
insert into T
select x, y, 10 from table(d);
end;
/
Комментариев нет:
Отправить комментарий