dineshbalajibingo
dineshbalajibingo

Reputation: 105

Need help in converting Oracle trigger to PostgreSQL

I need help in converting the below query to postgres. Any help is greatly appreciated

 CREATE TRIGGER REQUESTOR_TRG
    BEFORE INSERT ON REQUESTOR 
    FOR EACH ROW 
    BEGIN
      <<COLUMN_SEQUENCES>>
      BEGIN
        IF INSERTING AND :NEW.ID IS NULL THEN
          SELECT REQUESTOR_SEQ.NEXTVAL INTO :NEW.ID FROM SYS.DUAL;
        END IF;
      END COLUMN_SEQUENCES;
    END; 

Upvotes: 0

Views: 249

Answers (2)

Kaushik Nayak
Kaushik Nayak

Reputation: 31726

In case you want to use a desired sequence in Postgres, you may do.

create sequence REQUESTOR_SEQ;
create table REQUESTOR ( id int DEFAULT NEXTVAL('requestor_seq') )

Otherwise a serial column would suffice.

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1271151

In Postgres, you don't use sequences for this purpose. Just use a serial column and dispense with the trigger:

create table requestor (
    id serial primary key,
    . . .
);

Upvotes: 1

Related Questions