有幾種方法可以做到這一點。首先,你可以使用一個匿名塊或者一個嵌套的語句表或者一個關聯的語句數組。嵌套表方法對現有腳本的更改稍微少一些,但存在數字失序的風險。請注意,我正在使用替代引用機制,例如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