如何使用批量收集填充具有多個%rowtype字段的記錄表?使用具有多個%rowtype字段的Oracle PL/SQL記錄表
我的代碼:
drop table child_table;
drop table parent_table;
/
create table parent_table(pk number primary key);
create table child_table(pk number primary key, fk REFERENCES parent_table(pk));
/
insert into parent_table (pk) values (1);
insert into parent_table (pk) values (2);
insert into child_table (pk, fk) values (11, 1);
insert into child_table (pk, fk) values (21, 1);
insert into child_table (pk, fk) values (32, 2);
/
declare
type rec is record
(
parent parent_table%rowtype,
child child_table%rowtype
);
type tbl is table of rec;
v_table tbl := tbl();
-- this works
type tbl_parent is table of parent_table%rowtype;
v_parent_table tbl_parent := tbl_parent();
begin
-- this works
select * bulk collect into v_parent_table from parent_table;
-- this doesn't work
select * bulk collect into v_table from parent_table parent
inner join child_table child on parent.pk = child.fk;
end;
這個代碼不工作,但引發以下錯誤消息:
ORA-06550: line 13, column 30:
PLS-00597: expression 'V_TABLE' in the INTO list is of wrong type
ORA-06550: line 13, column 30:
PLS-00597: expression 'V_TABLE' in the INTO list is of wrong type
ORA-06550: line 13, column 38:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 13, column 3:
PL/SQL: SQL Statement ignored
這樣就ok了,Oracle說使用im了錯誤的數據類型,我同意。但如何解決它?
'SELECT * FROM聯接B'不給你,你可以拆分這樣的獨立行,如我認爲你意識到。是否有理由不能在記錄定義中使用'%type'而不是'%rowtype'來明確列出這兩個表中的字段(或者至少是你真正想要檢索的字段)?除了它需要更多的打字,顯然...... –