求大神幫忙寫個觸發器:
表名A8104,字段:A81041
我需要這個字段的格式:ER00001,然後ER00002,ER00003……這樣自動生成
就剩一個幣了
-- 建表
CREATE TABLE A8104(
A81041 VARCHAR2(10) NOT NULL,
FIELD NUMBER(6)
);
-- 序列
CREATE SEQUENCE SEQ_A8104
MINVALUE 1
MAXVALUE 99999999
START WITH 1
INCREMENT BY 1
NOCACHE;
-- 觸發器
CREATE OR REPLACE TRIGGER TRI_A8104_1 --觸發器名稱
BEFORE INSERT ON A8104
FOR EACH ROW
DECLARE
NEXTID VARCHAR2(10);
BEGIN
IF :NEW.A81041 IS NULL OR :NEW.A81041 = 0 THEN
SELECT 'E' || to_char(SEQ_A8104.NEXTVAL, 'FM000000') --SEQ_A8104正是剛才創建的序列
INTO NEXTID
FROM SYS.DUAL;
:NEW.A81041 := NEXTID;
END IF;
END TRI_A8104_1;
INSERT INTO A8104(FIELD) VALUES(10);