-----------------------动态创建序列的脚本---------------------------
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中能正确执行,同时程序通过斜杠分割之后也能够正确执行。