存储过程
2017-03-30 22:39 安松 阅读(197) 评论(0) 编辑 收藏 举报基本语法:
create or replace procedure create_businesscgdmx(year in varchar2) Authid Current_User as // 红:参数 蓝:当前用户 v_tab_sl number; //声明变量 yycgdmx varchar2(100); yycgd varchar2(100); business varchar2(100); str varchar2(1000); begin -- 表名称 yycgd := 'yycgd' || year; // “:=” 赋值 “||” 连接字符串 yycgdmx := 'yycgdmx' || year; business :='yybusiness'||year; -- 验证表是否存在 select count(1) into v_tab_sl from user_tables where table_name=upper(yycgdmx); if v_tab_sl = 0 then str := 'create table ' || yycgdmx || '(ID VARCHAR2(32) not null,' || 'yycgdid VARCHAR2(32) not null,' || 'usergysid VARCHAR2(64) not null,' || 'ypxxid VARCHAR2(32) not null,' || 'zbjg FLOAT not null,' || 'jyjg FLOAT ,' || 'cgl INTEGER ,' || 'cgje FLOAT ,' || 'cgzt CHAR(1) not null,' || 'vchar1 VARCHAR2(64),' || 'vchar2 VARCHAR2(64),' || 'vchar3 VARCHAR2(64),' || 'vchar4 VARCHAR2(128),' || 'vchar5 VARCHAR2(128)' || ')'; execute immediate str; str := 'alter table '||yycgdmx||' add constraint PK_'||yycgdmx||' primary key (ID)'; execute immediate str; str := 'alter table '||yycgdmx||' add constraint UNI_'||yycgdmx||' unique (YYCGDID, YPXXID)'; execute immediate str; str := 'alter table '||yycgdmx||' add constraint FK_'||yycgdmx||'_1 foreign key (YYCGDID) references '||yycgd||' (ID)'; execute immediate str; str := 'alter table '||yycgdmx||' add constraint FK_'||yycgdmx||'_3 foreign key (YPXXID) references YPXX (ID)'; execute immediate str; str :='create or replace trigger '||yycgdmx||'_update ' || // 触发器 ' after update on ' || yycgdmx || ' for each row ' || 'declare ' || 'begin ' || 'update '||business||' t ' || 'set ' || 't.zbjg = :new.zbjg,' || 't.jyjg = :new.jyjg,' || 't.cgzt = :new.cgzt,' || 't.cgl = :new.cgl ' || ' where t.yycgdid = :new.yycgdid and t.ypxxid = :new.ypxxid;'|| 'end '||yycgdmx||'_update ;'; execute immediate str; end if; end create_businesscgdmx;
execute immediate:简单来说 就是你一个存储过程当中 创建了一个表 table_a 然后要用insert into将其他的数据插入到这个table_a当中,但是因为你在创建过程的时候 table_a还不存在过程就会显示有编译错误,因为table_a不存在必然导致过程无法执行,所以无法编译成功,而把insert into语句加如到 execute immediate之后 则oracle不会再去理会这个对象是否存在,
因此可以成功编译和执行。
2. 在plsql,存储过程或其他对象如函数、触发器红叉有几种可能:
①、程序本身存在编译错误,即编译未通过,此时需查看具体错误以改正。
②、程序长时间未调用后失效需重新编译(无需手动编译)。属正常现象。
③、主程序中调用的其他程序做过改动和编译,主程序未即时编译。属正常现象,重新编译即可!