使用Execute Immediate
for FORALL
,BULK
收集 pl/sql 块:
FORALL i in rowid.FIRST ..rowid.LAST
Execute Immediate 'Delete table_name where rowid '=rowid(i)
上面的东西是行不通的。谁能给出insert
and的语法delete
?
以下是如何实现此目的的示例:
create table brainoverflow as ( select * from all_objects );
select count(*) from brainoverflow;
declare
type ri is table of rowid;
rowids ri;
begin
select rowid bulk collect into rowids from brainoverflow;
for i in rowids.first .. rowids.last loop
execute immediate ('delete brainoverflow where rowid='''||rowids(i)||'''');
end loop;
end;
/
select count(*) from brainoverflow;
在 SQL*Plus 中执行:
oracle@stormwind:~$ sqlplus phil/phil
SQL*Plus: Release 11.2.0.3.0 Production on Fri May 25 11:37:40 2012
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table brainoverflow as ( select * from all_objects );
Table created.
SQL> select count(*) from brainoverflow;
COUNT(*)
----------
72440
SQL> declare
2 type ri is table of rowid;
3 rowids ri;
4 begin
5 select rowid bulk collect into rowids from brainoverflow;
6 for i in rowids.first .. rowids.last loop
7 execute immediate ('delete brainoverflow where rowid='''||rowids(i)||'''');
8 end loop;
9 end;
10 /
PL/SQL procedure successfully completed.
SQL> select count(*) from brainoverflow;
COUNT(*)
----------
0
SQL>