-----------------------動態建立序列的腳本--------------------------- declare type num_list is varray(14) of varchar2(8); -- 老師表、學生表 xz num_list := num_list('TEACHER','STUDENT'); maxid integer:=1; have integer; begin for i in 1..xz.count loop --循環 execute immediate 'select count(1) from all_sequences where sequence_name = ''SEQ_'|| xz(i) ||'_X_OID''' into have; if have=0 then execute immediate 'select nvl(max(x_oid),1)+1 from gddbms.' || xz(i) into maxid; execute immediate 'create sequence gddbms.seq_'|| xz(i) ||'_x_oid start with ' ||maxid||' increment by 1 nomaxvalue'; end if; end loop; end; / -----------------------動態刪除序列的腳本--------------------------- declare type num_list is varray(18) of varchar2(8); xz num_list := num_list('TEACHER','STUDENT'); maxid integer:=1; have integer; begin for i in 1..xz.count loop --循環 execute immediate 'select count(1) from all_sequences where sequence_name = ''SEQ_'|| xz(i) ||'_X_OID''' into have; if have>0 then execute immediate 'drop sequence gddbms.seq_'|| xz(i) ||'_x_oid'; end if; end loop; end;
總結一下這裏面大概有以下幾個知識點:
1.num_list是定義的一個數組類型,xz是這個類型的一個對象,for用於循環xz對象。
2.execute immediate用於動態執行一串sql語句。
3.execute immediate **** into *** 用於將執行的結果賦值給某個變量。
4.if xxx then xxxx end if 就是if條件判斷。
5.如上,咱們每一個語句之間用斜槓(/)分割,在pl/sql中能正確執行,同時程序經過斜槓分割以後也可以正確執行。sql