Solved

Auto Increment an ID via a Trigger

Posted on 2009-07-13
8
427 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

932 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

9 Experts available now in Live!

Get 1:1 Help Now