DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: Oracle Trigger

  1. #1
    Join Date
    Jul 2008
    Posts
    5

    Oracle Trigger

    I am new to writing triggers and stored procedures and such, so I was wondering if anyone would be able to help me.

    I am trying to write a trigger so that once a record is inserted into a table called ABT_HOLD, that the trigger will look at a field called STRCS and if that field contains the word "Curved", then it will enter a 1 into a field called CURVED. If it not then it will enter a 0 into the field. If the same field called STRCS contains the word "Straight" then it will enter a 1 into a field called STRAIGHT.
    Also, along the same lines I want it to look at a field called STRSF and if that field contains the word "Snout", then it will enter a 1 into a field called SNOUT. If not put a 0 into the field. If the same field called STRSF contains the word "Fin" then it will enter a 1 into a field called FIN.

    Here is what I have so far.
    CREATE OR REPLACE TRIGGER tri_abt_hold
    BEFORE INSERT
    ON abt_hold
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    Begin
    IF :OLD.STRCS = 'Curved' THEN
    update ABT_HOLD Set :OLD.CURVED = 1;
    ELSE
    update ABT_HOLD Set :OLD.CURVED = 0;
    END IF;

    IF :OLD.STRCS = 'Straight' THEN
    update ABT_HOLD Set :OLD.STRAIGHT = 1;
    ELSE
    update ABT_HOLD Set :OLD.STRAIGHT = 0;
    END IF;

    IF :OLD.STRSF = 'Snout' THEN
    update ABT_HOLD Set :OLD.SNOUT = 1;
    ELSE
    update ABT_HOLD Set :OLD.SNOUT = 0;
    END IF;

    IF :OLD.STRSF = 'Fin' THEN
    update ABT_HOLD Set :OLD.FIN = 1;
    ELSE
    update ABT_HOLD Set :OLD.FIN = 0;
    END IF;
    End;
    /

    I am getting an error on this line "update ABT_HOLD Set :OLD.CURVED = 1;" saying "(1):PL/SQL: ORA-01747: invalid user.table.column, table.column, or column specification"
    If I take the ":OLD." off, then I don't get that error, but I get a mutating table error.

    Am I going about this the correct way? Seems like I need the :OLD in there...
    Any help would be appreciated.
    Thanks in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    mmmhhh... referencing the table that owns the triggers in a before trigger?that's bad -very bad mojo.

    Your trigger is looking for troubles, specifically "mutating table" troubles.

    Research "mutating table and triggers"
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Mar 2000
    Location
    Atlanta, GA,USA
    Posts
    155
    You can accomplish everything explained in your post without triggers and with much better performance using default value feature. Read SQL Reference book on create table statement. You need to look for column definition and then default value in it.
    Using default values is much cleaner way to do it and self documenting via Oracle data dictionary. Triggers are being abused by developers a lot. If I can make Oracle remove one feature from the database this would be object level DML triggers.

  4. #4
    Join Date
    Jul 2008
    Posts
    5
    Default values cannot contain references to other columns, so I don't understand how adding a default value would help in my situation. Can you provide an example?

    I figured out the necessary syntax to accomplish what I need with a trigger. However, if there is a better way to do it, I'd be glad to try it.

  5. #5
    Join Date
    Mar 2000
    Location
    Atlanta, GA,USA
    Posts
    155
    My bad. Anything wrong with doing it in the insert command itself? The logic to populate the columns is very simple. Perhaps, creating check constraints will be a very good idea to enforce data integrity and help optimizer a little bit.

  6. #6
    Join Date
    Jul 2008
    Posts
    5
    Unfortunately, the insert statement, or "export" as they call it, is coming from an OCR COTS product, where performing simple logic to manipulate the insert values on that side is very limited, or maybe I just can't seem to figure that out. So it seems like it is pretty much impossible to decipher from the COTS side if a certain field contains a certain value, then put another value in another field to be inserted into the database.

    I would agree though, doing it in the insert would be a better design and much more efficient.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width