有几种方法可以做到这一点。首先,你可以使用一个匿名块或者一个嵌套的语句表或者一个关联的语句数组。嵌套表方法对现有脚本的更改稍微少一些,但存在数字失序的风险。请注意,我正在使用替代引用机制,例如q'[character] ... [character]',以防万一你的DDL包含一些撇号。
该代码类似于其他一些答案,但是使用Oracle语法并且不需要创建其他对象。
嵌套表脚本:
--Only create the tables between the two values (nested table)
declare
type varchar_tab is table of varchar2(32767);
table_statements varchar_tab := varchar_tab(
q'!create table tab1 (test1 number)!',
q'!create table tab2 (test1 number)!',
q'!create table tab3 (test1 number)!',
q'!create table tab4 (test1 number)!'
);
begin
for i in &1 .. &2 loop
execute immediate table_statements(i);
end loop;
end;
/
关联数组脚本:
--Only create the tables between the two values (associative array)
declare
type varchar_tab is table of varchar2(32767) index by number;
table_statements varchar_tab;
begin
table_statements(1) := q'!create table tab1 (test1 number)!';
table_statements(2) := q'!create table tab2 (test1 number)!';
table_statements(3) := q'!create table tab3 (test1 number)!';
table_statements(4) := q'!create table tab4 (test1 number)!';
--Only create the tables between the two values
for i in &1 .. &2 loop
execute immediate table_statements(i);
end loop;
end;
/
如果你正在寻找一种方式,让你的脚本几乎等同于目前的形式,另一种方法是将运行整个脚本,然后在结尾放置不需要的表的匿名块。这样可以使脚本的顶部非常简单,但显然可能会有一些自动删除表的问题。
--Drop all the tables except for those within the range
declare
table_does_not_exist exception;
pragma exception_init(table_does_not_exist, -00942);
begin
for i in 1 .. 1000 loop
if i between &1 and &2 then
null;
else
begin
execute immediate 'drop table tab'||i;
exception when table_does_not_exist then null;
end;
end if;
end loop;
end;
/
它必须是.sql文件吗?或者你可以使用shell脚本 - 因为这对我来说似乎是最简单的方法。 – Benubird 2010-11-29 13:16:17