时间:2021-07-01 10:21:17 帮助过:108人阅读
关键语法: INSERT INTO TABLE VALUES(,) RETURNING ID INTO VAL
补充:此种情况,触发器的语句可能如下
CREATE OR REPLACE TRIGGER GPS.TR_EM_FILES before insert on "GPS"."EM_FILES" for each row begin select "GPS"."SQ_EM_FILES".nextval into :new."ID" from dual; end; /
或
oralce12c,主键是通过default设置SEQUENCE下一个值
CREATE TABLE C##EM.GP_TARGET_TYPE ( ID NUMBER(19) DEFAULT "C##EM"."ISEQ$$_124895".nextval NOT NULL, --DEFAULT默认值为seq的下个值 NAME NVARCHAR2(50) NOT NULL, REMARK NVARCHAR2(200), CREATE_TIME DATE NOT NULL, CREATE_UID NUMBER(19), DELETE_UID NUMBER(19), DELETE_TIME DATE, IS_DELETE NUMBER(1) NOT NULL, UPDATE_TIME DATE, UPDATE_UID NUMBER(19) )
2)如果主键ID的值由用户自主获得SEQUENCE的值,然后赋值,实现方式可以如下:
SELECT YOUR_SEQ.NEXTVAL INTO MID_ID FROM DUAL; --MID_ID为变量 INSERT INTO TABLE(ID) VALUES(MID_ID);
补充:此种情况,可以有触发器,语句可能如下:
CREATE OR REPLACE TRIGGER C##WL.TR_BULLETIN BEFORE INSERT ON C##WL.WL_BULLETIN FOR EACH ROW WHEN ( NEW.ID IS NULL OR NEW.ID = 0 ) BEGIN SELECT WL_BULLETIN_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL; END; /
当前插入的主键ID为空值或0时,获取对应SEQUENCE 的NEXTVAL值来赋值主键ID
Oracle 在函数或存储过程中执行一条插入语句并返回主键ID值
标签:sequence enc family varchar char end rom 自增 sele