Home » SQL & PL/SQL » SQL & PL/SQL » USING A SEQUENCE IN A TRIGGER
USING A SEQUENCE IN A TRIGGER [message #40781] Wed, 06 November 2002 09:37 Go to next message
Adi
Messages: 38
Registered: May 2002
Member
Hi all
I am trying to insert a Sequence value into a field, all this inside my Trigger. I am relatively new to this field, so Please correct any mistakes in the Trigger below:

CREATE OR REPLACE TRIGGER MY_TRIGGER
BEFORE INSERT
ON SCHEMA.TABLE1
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
i NUMBER;
BEGIN
IF INSERTING THEN
:new.ID_NUM := (select SCHEMA.ID_SEQ.NEXTVAL into :i from dual);
:new.CREATED_DATE := SYSDATE;
END IF;
END;

I also tried this:
:new.ID_NUM := SCHEMA.ID_SEQ.NEXTVAL
but the error says 'Sequence not allowed in this context'

Thanks
ADI
Re: USING A SEQUENCE IN A TRIGGER [message #40783 is a reply to message #40781] Wed, 06 November 2002 09:48 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
create table tab1 ( col1 number primary key, col2 number );

create sequence tab1_seq;
-- Before Insert for Each Row
CREATE OR REPLACE TRIGGER tab1_bier
   BEFORE INSERT
   ON tab1
   FOR EACH ROW
BEGIN
   IF (:NEW.col1 IS NULL)
   THEN
      SELECT tab1_seq.NEXTVAL
        INTO :NEW.col1
        FROM DUAL;
   END IF;
END;
/


insert into tab1 (col2) values (33);
insert into tab1 (col2) values (99);

select * from tab1;
1 33
2 99
Re: USING A SEQUENCE IN A TRIGGER [message #40786 is a reply to message #40781] Wed, 06 November 2002 10:36 Go to previous messageGo to next message
sai sreenivas jeedigunta
Messages: 370
Registered: November 2001
Senior Member
Hi,
the error is in the way u wrote the statement..try in the below way

IF INSERTING THEN
select SCHEMA.ID_SEQ.NEXTVAL into :new.id_num from dual;
:i:=:new.id_num;
:new.CREATED_DATE := SYSDATE;
END IF;

check if that works !
sai
Re: USING A SEQUENCE IN A TRIGGER [message #40791 is a reply to message #40781] Wed, 06 November 2002 11:23 Go to previous message
Adi
Messages: 38
Registered: May 2002
Member
Thanks Srinivas and Andrew, that was really helpful.
Previous Topic: how can I get order of table colomns
Next Topic: views
Goto Forum:
  


Current Time: Wed May 08 01:20:33 CDT 2024