如何在 oracle 中使用游标的批量收集获取 table 的所有列的 Rowid
How to fetch the Rowid with all the columns of a table using bulk collect for a cursor in oracle
declare
cursor c_1 is select a.*, a.rowid from an_test a;
type t_1 is table of an_test%rowtype;
type l_row_id is table of UROWID;
tab t_1;
row l_row_id;
begin
open c_1;
loop
fetch c_1 bulk collect into tab, row limit 1000;
...
end loop;
end;
我正在尝试执行上面的代码,但它给我这样的错误:
PLS-00597: expression 'tab' in the INTO list is of wrong type.
有什么other/alternate方法可以做到这一点吗?
提前致谢。
应该是这样的:
SQL> set serveroutput on;
SQL> declare
2 cursor c1 is select d.*, d.rowid from dept d;
3 type t1r is record
4 (deptno number,
5 dname varchar2(20),
6 loc varchar2(20),
7 rowid urowid
8 );
9 type t1t is table of t1r;
10 t1 t1t;
11 begin
12 open c1;
13 fetch c1 bulk collect into t1;
14 close c1;
15
16 for i in t1.first .. t1.last loop
17 dbms_output.put_line(rpad(t1(i).dname, 15, ' ') ||' '|| t1(i).rowid);
18 end loop;
19 end;
20 /
ACCOUNTING AAAGvqAAEAAAAIUAAA
RESEARCH AAAGvqAAEAAAAIUAAB
SALES AAAGvqAAEAAAAIUAAC
OPERATIONS AAAGvqAAEAAAAIUAAD
PL/SQL procedure successfully completed.
SQL>
在这种情况下,您不需要额外的 rowid 变量。只需从中删除 , row
fetch c_1 bulk collect into tab, row limit 1000;
应该很简单
fetch c_1 bulk collect into tab limit 1000;
如果您可以在同一记录中使用 rowid 进行管理,请将您的类型基于游标而不是 table:
declare
cursor c is
select a.*, a.rowid
from customer a;
type t is table of c%rowtype;
tab t;
begin
open c;
fetch c bulk collect into tab limit 1000;
end;
SQL> set serveroutput on;
SQL> declare
cursor c1 is select d.*, d.rowid from dept d;
type t1r is record
(deptno number,
dname varchar2(20),
loc varchar2(20),
rowid urowid
);
type t1t is table of t1r;
t1 t1t;
begin
open c1;
fetch c1 bulk collect into t1;
close c1;
for i in t1.first .. t1.last loop
dbms_output.put_line(rpad(t1(i).dname, 15, ' ') ||' '|| t1(i).rowid);
end loop;
end;
/
declare
cursor c_1 is select a.*, a.rowid from an_test a;
type t_1 is table of an_test%rowtype;
type l_row_id is table of UROWID;
tab t_1;
row l_row_id;
begin
open c_1;
loop
fetch c_1 bulk collect into tab, row limit 1000;
...
end loop;
end;
我正在尝试执行上面的代码,但它给我这样的错误:
PLS-00597: expression 'tab' in the INTO list is of wrong type.
有什么other/alternate方法可以做到这一点吗?
提前致谢。
应该是这样的:
SQL> set serveroutput on;
SQL> declare
2 cursor c1 is select d.*, d.rowid from dept d;
3 type t1r is record
4 (deptno number,
5 dname varchar2(20),
6 loc varchar2(20),
7 rowid urowid
8 );
9 type t1t is table of t1r;
10 t1 t1t;
11 begin
12 open c1;
13 fetch c1 bulk collect into t1;
14 close c1;
15
16 for i in t1.first .. t1.last loop
17 dbms_output.put_line(rpad(t1(i).dname, 15, ' ') ||' '|| t1(i).rowid);
18 end loop;
19 end;
20 /
ACCOUNTING AAAGvqAAEAAAAIUAAA
RESEARCH AAAGvqAAEAAAAIUAAB
SALES AAAGvqAAEAAAAIUAAC
OPERATIONS AAAGvqAAEAAAAIUAAD
PL/SQL procedure successfully completed.
SQL>
在这种情况下,您不需要额外的 rowid 变量。只需从中删除 , row
fetch c_1 bulk collect into tab, row limit 1000;
应该很简单
fetch c_1 bulk collect into tab limit 1000;
如果您可以在同一记录中使用 rowid 进行管理,请将您的类型基于游标而不是 table:
declare
cursor c is
select a.*, a.rowid
from customer a;
type t is table of c%rowtype;
tab t;
begin
open c;
fetch c bulk collect into tab limit 1000;
end;
SQL> set serveroutput on;
SQL> declare
cursor c1 is select d.*, d.rowid from dept d;
type t1r is record
(deptno number,
dname varchar2(20),
loc varchar2(20),
rowid urowid
);
type t1t is table of t1r;
t1 t1t;
begin
open c1;
fetch c1 bulk collect into t1;
close c1;
for i in t1.first .. t1.last loop
dbms_output.put_line(rpad(t1(i).dname, 15, ' ') ||' '|| t1(i).rowid);
end loop;
end;
/