Solved

Auto Increment an ID via a Trigger

Posted on 2009-07-13
8
432 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
Technology Partners: 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!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

751 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