Execute Immediate
用于FORALL
,BULK
收集在pl / sql块中:
FORALL i in rowid.FIRST ..rowid.LAST
Execute Immediate 'Delete table_name where rowid '=rowid(i)
以上的事情是行不通的。任何人都可以为insert
和delete
提供语法吗?
答案 0 :(得分:1)
以下是如何实现此目标的示例:
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>