连续创建多个Oracle触发器失败,单个创建才成功的解决方法

当用到自增序列,需要用到触发器的时候,当我连续执行创建多个触发器时,总是报编译通过,但存在警告或错误。

---1.应用信息
drop table app_info cascade constraints;
create table APP_INFO
(
  id      NUMBER(9)   not null,
  appid    VARCHAR2(255)        default NULL,
  appkey    VARCHAR2(255)       default NULL,
  md5_key          VARCHAR2(255)    default NULL,
  aes_key    VARCHAR2(255)      default NULL,
  permission    VARCHAR2(3000)     default NULL,
  email    VARCHAR2(255)       default NULL,
  password    VARCHAR2(255)      default NULL,
  ras_public_key     VARCHAR2(255)  default NULL,
  constraint APP_INFO primary key (id)
);

drop sequence app_info_id;
create sequence app_info_id
increment by 1
start with 1
nomaxvalue
nominvalue
nocache;

CREATE OR REPLACE TRIGGER addAppInfo BEFORE INSERT ON App_INFO FOR EACH ROW  
 BEGIN  
    SELECT app_info_id.nextval INTO :new.id  FROM dual;  
 END; 

---2.unit信息
drop table unit_info cascade constraints;
create table UNIT_INFO
(
  UNIT_ID        NUMBER(9)  not null,
  USERID          VARCHAR2(255)        default NULL,
  UNIT_NAME      VARCHAR2(128)        default NULL,
  UNIT_LINKMAN      VARCHAR2(64)    default NULL,
  ORGANIZATION_CODE        VARCHAR2(64)     default NULL,
  constraint UNIT_INFO primary key (UNIT_ID)
);

drop sequence unit_info_id;
create sequence unit_info_id
increment by 1
start with 1
nomaxvalue
nominvalue
nocache;

CREATE OR REPLACE TRIGGER addUnitInfo BEFORE INSERT ON UNIT_INFO FOR EACH ROW  
 BEGIN  
    SELECT unit_info_id.nextval INTO :new.unit_id  FROM dual;  
 END; 

错误截图如下:

这种创建是失败的,因为触发器需要先编译,每创建一个触发器,需要以“/”结束,然后才能执行下一个。

正确的做法应该是这样的;

---1.应用信息
drop table app_info cascade constraints;
create table APP_INFO
(
  id      NUMBER(9)   not null,
  appid    VARCHAR2(255)        default NULL,
  appkey    VARCHAR2(255)       default NULL,
  md5_key          VARCHAR2(255)    default NULL,
  aes_key    VARCHAR2(255)      default NULL,
  permission    VARCHAR2(3000)     default NULL,
  email    VARCHAR2(255)       default NULL,
  password    VARCHAR2(255)      default NULL,
  ras_public_key     VARCHAR2(255)  default NULL,
  constraint APP_INFO primary key (id)
);

drop sequence app_info_id;
create sequence app_info_id
increment by 1
start with 1
nomaxvalue
nominvalue
nocache;

CREATE OR REPLACE TRIGGER addAppInfo BEFORE INSERT ON App_INFO FOR EACH ROW  
 BEGIN  
    SELECT app_info_id.nextval INTO :new.id  FROM dual;  
 END; 
/
---2.unit信息
drop table unit_info cascade constraints;
create table UNIT_INFO
(
  UNIT_ID        NUMBER(9)  not null,
  USERID          VARCHAR2(255)        default NULL,
  UNIT_NAME      VARCHAR2(128)        default NULL,
  UNIT_LINKMAN      VARCHAR2(64)    default NULL,
  ORGANIZATION_CODE        VARCHAR2(64)     default NULL,
  constraint UNIT_INFO primary key (UNIT_ID)
);

drop sequence unit_info_id;
create sequence unit_info_id
increment by 1
start with 1
nomaxvalue
nominvalue
nocache;

CREATE OR REPLACE TRIGGER addUnitInfo BEFORE INSERT ON UNIT_INFO FOR EACH ROW  
 BEGIN  
    SELECT unit_info_id.nextval INTO :new.unit_id  FROM dual;  
 END; 
/

 注意:存储过程也是一样的。

posted @ 2017-12-11 17:15  shawWey  阅读(1513)  评论(0编辑  收藏  举报