Solved

Auto Increment an ID via a Trigger

Posted on 2009-07-13
8
426 Views
Last Modified: 2012-05-07
I have this trigger that works below.  In the audit table, audit_lu_bld, there is an AuditID, that I had incrementing by 1 each time a new row was inserted.  I realize in Oracle that you use sequences to do this but is there a way to include the sequence code in the trigger itself?

Thx


CREATE OR REPLACE TRIGGER tr_lu_building_group
    BEFORE DELETE OR UPDATE OR INSERT
    ON lu_bld
    FOR EACH ROW
DECLARE
    actionid INTEGER;
BEGIN
    IF INSERTING
    THEN
        actionid := 2;
 
        INSERT INTO audit_lu_bld(
                                     actionid,
                                     modified_date,
                                     table_name,
                                     userid,
                                     bldid,
                                     bld_no
                   )
        VALUES     (
                        actionid,
                        SYSDATE,
                        'LU_Bld',
                        :new.userid,
                        :new.bldid,
                        :new.bld_no
                   );
    ELSIF UPDATING OR DELETING
    THEN
        IF UPDATING
        THEN
            actionid := 1;
        ELSE
            actionid := 3;
        END IF;
 
 
        INSERT INTO audit_lu_bld(
                                     actionid,
                                     modified_date,
                                     table_name,
                                     userid,
                                     bldid,
                                     bld_no
                   )
        VALUES     (
                        actionid,
                        SYSDATE,
                        'LU_Bld',
                        :old.userid,
                        :old.bldid,
                        :old.bld_no
                   );
    END IF;
END;
0
Comment
Question by:Glen_D
  • 4
  • 2
  • 2
8 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 300 total points
ID: 24841571

I don't see where you are inserting AuditID above. You can use the sequence multiple ways inside the trigger.

Simply naming the sequence in the VALUES() clause is usually sufficient

INSERT INTO audit_lu_bkd(auditID, ....)
VALUES(audit_lu_seq.nextval, ...)
0
 
LVL 47

Assisted Solution

by:schwertner
schwertner earned 200 total points
ID: 24841583
First you have to define the trigger:

CREATE SEQUENCE dept
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE;

In the the trigger you should put
select dept.nextval into :new.column_name:

If you are speking about the INSERT statement the put in the VALUE clause of INSERT the value  dept.nextval

You have the indacate where you need (statement, column name) the sequence value.

0
 

Author Comment

by:Glen_D
ID: 24841687
OK...I do need to create the sequence first and then insert as below...this may be correct...thx

drop sequence  seq_LU_Building_Group;
create sequence seq_LU_Building_Group start with 1 increment by 1 nocache;

& then call the seq in the trigger code as:

CREATE OR REPLACE TRIGGER tr_lu_building_group
    BEFORE DELETE OR UPDATE OR INSERT
    ON LU_Building_Group
    FOR EACH ROW
DECLARE
    actionid INTEGER;
BEGIN
    IF INSERTING
    THEN
        actionid := 2;
 
        INSERT INTO Audit_LU_Building_Group(
                                                                                                           auditid,
                              bldgroupid,
                                   bld_grp,
                                   centerid,
                                   userid,
                                   actionid,
                                           modified_date,
                                           table_name
                                     
                                     
                                     
                   )
        VALUES     (
                        seq_LU_Building_Group.nextval
                  :new.bldgroupid,
                  :new.bld_grp,
                  :new:centerid,
                  :new,userid,
                  actionid,
                  SYSDATE,
                  'LU_Building_Group'
                       
                   );
    ELSIF UPDATING OR DELETING
    THEN
        IF UPDATING
        THEN
            actionid := 1;
        ELSE
            actionid := 3;
        END IF;
 
 
        INSERT INTO audit_lu_bld(
                             auditid,
                                     bldgroupid,
                             bld_grp,
                             centerid,
                             userid,
                             actionid,
                                     modified_date,
                                     table_name
                   )
        VALUES     (
                        seq_LU_Building_Group.nextval
                  :old.bldgroupid,
                  :old.bld_grp,
                  :old:centerid,
                  :old,userid,
                  actionid,
                  SYSDATE,
                  'LU_Building_Group'
                   );
    END IF;
END;

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24841731
The above trigger will create 2 ids, one after the other. Do you want the value inserted in audit_lu_bld to be the same as the Audit_LU_Building_Group table?

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24841754
To use the same value in 2 different inserts, use .nextval first, then use .currval for all subsequent, which will reuse the current value without incrementing.

begin
  insert into s values(seq.nextval);
  insert into t values(seq.currval);
  insert into u values(seq.currval);
  end;
/
0
 

Author Comment

by:Glen_D
ID: 24841788
That was my mistake...for i,u,d...the values for this trigger will be inserted into the same audit table so nextval would be appropriate.  Thx....is this correct?

CREATE OR REPLACE TRIGGER tr_lu_building_group
    BEFORE DELETE OR UPDATE OR INSERT
    ON LU_Building_Group
    FOR EACH ROW
DECLARE
    actionid INTEGER;
BEGIN
    IF INSERTING
    THEN
        actionid := 2;
 
        INSERT INTO Audit_LU_Building_Group(
                                           auditid,
                              bldgroupid,
                                   bld_grp,
                                   centerid,
                                   userid,
                                   actionid,
                                           modified_date,
                                           table_name
                                     
                                     
                                     
                   )
        VALUES     (
                        seq_LU_Building_Group.nextval
                  :new.bldgroupid,
                  :new.bld_grp,
                  :new:centerid,
                  :new,userid,
                  actionid,
                  SYSDATE,
                  'LU_Building_Group'
                       
                   );
    ELSIF UPDATING OR DELETING
    THEN
        IF UPDATING
        THEN
            actionid := 1;
        ELSE
            actionid := 3;
        END IF;
 
 
        INSERT INTO Audit_LU_Building_Group(
                             auditid,
                                     bldgroupid,
                             bld_grp,
                             centerid,
                             userid,
                             actionid,
                                     modified_date,
                                     table_name
                   )
        VALUES     (
                        seq_LU_Building_Group.nextval
                  :old.bldgroupid,
                  :old.bld_grp,
                  :old:centerid,
                  :old,userid,
                  actionid,
                  SYSDATE,
                  'LU_Building_Group'
                   );
    END IF;
END;
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24841804
Yes, nextval is in both inserts, that is correct.
0
 
LVL 47

Expert Comment

by:schwertner
ID: 24841817
Please clarify if you need for the two different tables different sequential values or same value.
The tables are different.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to recover a database from a user managed backup

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now