Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Auto Increment an ID via a Trigger

Posted on 2009-07-13
8
Medium Priority
?
437 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 1200 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 800 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

782 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