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

Thread: Trigger ?

  1. #1
    Join Date
    Sep 2000
    Posts
    64
    Hello,
    Suppose that you have a table T1 with columns C11, C12....
    When a new line is inserted, I would like modify the value of the field C11 if a given condition is satisfied : If C11 like '-%',
    I would like to add 'P' in the bgining of the C11 field :
    concat('P', OLD_C11_VALUE).

    Can I do that with a trigger and How ?

    Thanks a lot in advance.

    Sofiane


    Sofiane

  2. #2
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    Sure, a trigger is the perfect method for that. You will be using a before insert trigger. In the body of the trigger, you can inspect the c11 value and change it before it is inserted in the table.
    Jeff Hunter

  3. #3
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    Something like (syntax foggy, but you get the idea):

    create or replace trigger bi_t1
    before insert on t1 -- you are specifying you want this to fire before the insert happens
    for each row -- check every row
    begin
    if C11 = '-' then
    :new.C11 = 'P' || C11; -- assign the value using the builtin :new
    end;
    end;
    Jeff Hunter

  4. #4
    Join Date
    Sep 2000
    Posts
    64
    Hello,

    Thanks a lot for the reply, the right syntax is the following :


    CREATE OR REPLACE trigger bi_t1
    BEFORE INSERT ON T1
    FOR EACH RAW
    BEGIN
    IF (:NEW.C11 LIKE '-%') THEN
    :NEW.C11:=concat('P',:C11);
    END IF;
    END;

    Sofiane
    Sofiane

  5. #5
    Join Date
    Sep 2000
    Posts
    64
    Sorry, the right syntax is the following :


    CREATE OR REPLACE trigger bi_t1
    BEFORE INSERT ON T1
    FOR EACH RAW
    BEGIN
    IF (:NEW.C11 LIKE '-%') THEN
    :NEW.C11:=concat('P',:NEW.C11);
    END IF;
    END;

    Sofiane

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