Solved

Auto Increment an ID via a Trigger

Posted on 2009-07-13
8
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 48

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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 48

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

623 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