码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-12-11 18:38:55      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:pid   word   成功   strong   nocache   har   触发器   default   const   

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

---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; 
/

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

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

标签:pid   word   成功   strong   nocache   har   触发器   default   const   

原文地址:http://www.cnblogs.com/shawWey/p/8024095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!