|
|
Question : autoincrement field problem
|
|
i have create a table, a sequence and a trigger to use an autoincrement column: Table: create table schema1.Document ( NumDoc Number, NumProj char(10)); Sequence: create sequence schema1.autoincrement increment by 1 start with 1 minvalue 1 maxvalue 999999999 Trigger: create trigger schema1.test after insert on Document begin update Document set NumDoc=autoincrement.nextval; end;
when i try to insert records with: insert into schema1.Document (Numproj) values ('hello'); it worked but when i see what i have insert, i have nothing in the auto-increment column: select * from schema1.Document
NumDoc Numproj --------------------- 1
if i try to insert insert into schema1.Document (NumeroDoc, Numeroproj) values (autoincrement.nextval, 'hello');
i obtain ORA-02289: the sequence does not exist
i don't understand, somebody can help me please???
|
Answer : autoincrement field problem
|
|
Hello reivilo,
I've used this script to create sequence and autoincrement trigger:
SET VERIFY OFF
CREATE SEQUENCE table1_s START WITH 1 NOCACHE;
DEFINE TRIGGER_NAME="TABLE1_BRI" DEFINE TABLE_NAME="TABLE1"
ALTER TABLE &TABLE_NAME ENABLE ALL TRIGGERS;
CREATE OR REPLACE TRIGGER &TRIGGER_NAME BEFORE INSERT ON &TABLE_NAME FOR EACH ROW BEGIN
IF ( :new.id IS NULL ) THEN BEGIN SELECT table1_s.NEXTVAL INTO :new.id FROM DUAL; EXCEPTION WHEN OTHERS THEN NULL; END; END IF;
EXCEPTION WHEN OTHERS THEN NULL; END &TRIGGER_NAME; /
SHOW ERRORS TRIGGER &TRIGGER_NAME
Hope this helps, Paasky
|
|
|
|
|