Help on creating update trigger(urgent)

Hii all,
I have a situation like this
I have 10 different tables like a,b,c,d,e,f
But i have same columns in all tables
like
updated_by,
updated_date
I need to create a procedure and call that procedure in update triggers for all tables
Can anybody help
In creating Procedure and trigger
Thanks

There is nothing wrong with the trigger, but the procedure is another story. You cannot do DML on the table that is firing the trigger inside the trigger, that is the mutating table error.
I am not really sure why you are doing the DML anyway. you have defined a BEFORE UPDATE trigger on k_constituent, and the procedure checks for the existence of a row in the same table where the currt_user_id field is the same as the id of the user making the update. Without knowing anything about the application, this implies one of two things to me. Either you are trying to make sure the row to be updated does exist, or, this is some kind of security check.
If it is the first, then it is unneccessary, since the before update trigger will only fire if the row to be updated exists viz.
SQL> CREATE OR REPLACE TRIGGER jt_bu
  2  BEFORE UPDATE ON JTEST
  3  FOR EACH ROW
  4
  5  BEGIN
  6     :new.updby := USER;
  7     :new.updon := SYSDATE;
  8     DBMS_OUTPUT.Put_Line('Trigger fired ok');
  9  END;
10  /
Trigger created.
SQL> SELECT * FROM jtest;
        ID DESCR      UPDBY                     UPDON
         1 YES        OPS$ORACLE                24-APR-03
         2 NEWDES     OPS$ORACLE                23-APR-03
         3 ORACLE     OPS$ORACLE                23-APR-03
SQL> UPDATE jtest SET descr = 'NO' WHERE id = 1;
Trigger fired ok
1 row updated.
SQL>  UPDATE jtest SET descr = 'NO' WHERE id = 5;
0 rows updated.If you are doing this for security, then you are way too late. The user should not be in the database doing updates if they are not a valid user (whatever valid user means to you).
As a side note, you do realize that if there is more than one record in k_constituent with currt_user_id = updating user, then the procedure will not set update_date and update_by?
TTFN
John

Similar Messages

  • Help with create a trigger

    hello all
    i have a 4 tables and i would like to create a trigger in a tables
    CREATE TABLE CLIENT_INFO
    ( CLIENT_NO     VARCHAR2(10) CONSTRAINT CLIENT_INFO_CNO_PK PRIMARY KEY,
      CLIENT_NAME   VARCHAR2(50) NOT NULL,
      ORDERS_AMOUNT NUMBER(7)
    CREATE TABLE STOCK_INFO
    ( ITEM_NO              VARCHAR2(10) ,
      ITEM_DESCRIPTION     VARCHAR2(100),
      SELLING_PRICE        NUMBER(6),
      QTY_IN_HAND          NUMBER(6)    NOT NULL,
      CONSTRAINT ITEM_NUM_SPRICE_PK PRIMARY KEY (ITEM_NO , SELLING_PRICE)
    CREATE TABLE ORDER_INFO
    ( ORDER_NO     VARCHAR2(10) CONSTRAINT ORDER_INFO_ONO_PK PRIMARY KEY,
      CLIENT_NO    VARCHAR2(10),
      ORDER_DATE   DATE,
      ORDER_AMOUNT NUMBER(6),
      CONSTRAINT ORDER_INFO_CNO_FK  FOREIGN KEY (CLIENT_NO) REFERENCES CLIENT_INFO (CLIENT_NO)
    CREATE TABLE ORDER_LINES
    ( ORDER_NO       VARCHAR2(10),
      ITEM_NO        VARCHAR2(10),
      LINE_QTY       NUMBER(6),
      SELLING_PRICE  NUMBER(6),
      TOTAL_PRICE    NUMBER(6)
    ALTER TABLE ORDER_LINES
    ADD  CONSTRAINT ORDER_LINES_ONO_FK FOREIGN KEY (ORDER_NO) REFERENCES ORDER_INFO (ORDER_NO);
    ALTER TABLE ORDER_LINES
    ADD  CONSTRAINT ORDER_LINES_INO_FK FOREIGN KEY (ITEM_NO) REFERENCES STOCK_INFO (ITEM_NO);i would like to create this trigger
    1-order_amount in table 3 due to any (insert,update or delete ) in total_price in table 4
    2-orders_amount in table 1 due to any (insert,update or delete ) in order_amount in table 3
    i would like to ask another quotations r this relations in good for tables
    thank's all

    >
    plz i need a help to create a trigger
    >
    Using a trigger won't solve your problem. You are trying to use child table triggers to maintain parent table information.
    There is no transaction control to ensure that the parent table will be updated with the correct information.
    One process could update child record 1 while another process is updating child record two. Each process will see a different total if they try to compute the sum of all child records since the first process will see the 'old' value for the child record that the second process is updating and the second process will the 'old' value for the child record that the first process is updating.
    So the last process to commit could store the wrong total in the parent record withoug an exception ever being raised.
    See Conflicting Writes in Read Committed Transactions in the Database Concepts doc
    http://docs.oracle.com/cd/E14072_01/server.112/e10713/consist.htm
    >
    some one ask me this quotation in interview
    and im told him i can't understand this structure for database he said just do it
    >
    And I would tell them that using a trigger is not the right way to accomplish that since it could invalidate the data concurrency and data consistency of the tables.
    Sometimes you just have to tell people NO.

  • Need Help in creating this trigger

    I need your help to create this trigger. I need to set the default fl in this table depending on various conditions:
    If there is only one indvl with end date as null then set the default_pk column for that indvl as 'Y'
    ELSE
    If there are multiple indvl_pks in this table with NULL end date then set the default_fl column to 'Y' for the indvl_pk with the earliest start date .
    ELSE if there are multiple indvls with same start date then set the dflt_fl to 'Y' with the minimum br_pk
    I am unable to get this to work due to the mutating trigger problem.
    For example in this one rows with emplt_pk with 1001 and 1003 will be set to 'Y'.
    create table emplt
    emplt_pk number,
    indvl_pk number,
    start_dt date,
    end_dt date,
    lct_fl char(1),
    sup_fl char(1),
    br_pk number,
    nro_pk number,
    default_fl
    char(1) default 'N' );
    INSERT
    INTO emplt
    values(1001, 101, to_date ('01-01-2005', 'MM-DD-YYYY' ), NULL, 'Y','N' ,123,NULL,NULL );
    INSERT INTO emplt values(
    1002, 101, to_date ('02-01-2005', 'MM-DD-YYYY' ), NULL, 'Y','N' ,NULL,0001,NULL );
    INSERT INTO emplt values(
    1003, 102, to_date ('02-01-2005', 'MM-DD-YYYY' ), NULL, 'Y','N' ,NULL,0001,NULL );
    Thanks in advance

    the Easy Tabs could be useful for your requirement
    http://usermanagedsolutions.com/SharePoint-User-Toolkit/Pages/Easy-Tabs-v5.aspx
    /blog
    twttr @esjord

  • Basic help needed creating a trigger

    I'm completely new to SQL programming altogether, and I'm trying to set up a trigger that will make some checks before adding a new row to a given table. I am trying to do this because I need to enforce a CONSTRAINT that checks whether the dates entered for 'deadline' and 'startDate' are greater than sysdate and less than (sysdate + 365), and I found out that you cannot reference sysdate from a CONSTRAINT. Therefore I am now attempting to do this using a trigger, but I don't really know how to do this. Here is the sql code used to create the table:
    -- PLACEMENT TABLE
    DROP TABLE placement;
    CREATE TABLE placement
    contactId Int,
    placementId Int,
    position VARCHAR(60),
    description CLOB,
    lengMonths Int,
    salary Number(7,2),
    deadline DATE,
    startDate DATE,
    addrLine1 VARCHAR(120),
    postCd VARCHAR(10)
    And here is my attempt at creating the trigger that will only allow the deadline and startDate to be entered if they satisfy the following check: (sysdate < deadline/startDate <= sysdate+365)
    CREATE OR REPLACE TRIGGER trg_deadline_low BEFORE INSERT OR UPDATE OF deadline ON placement
    BEGIN
    IF :deadline <= SYSDATE THEN
    ROLLBACK;
    END IF;
    END;
    CREATE OR REPLACE TRIGGER trg_deadline_high BEFORE INSERT OR UPDATE OF deadline ON placement
    BEGIN
    IF :deadline > SYSDATE + 365 THEN
    ROLLBACK;
    END IF;
    END;
    If possible, I would like for these triggers to display an error rather than just using ROLLBACK, but I'm not sure how! At the moment, these triggers do not work at all; I get an error message "Warning: Trigger created with compilation errors." when I attempt to create the triggers in the first place.
    Can anyone tell me why I am seeing this error when trying to implement the triggers? And if anyone could also improve on my amateur attempt at coding a trigger, then that would be great! Thanks for any help!

    Oops!
    Nicolas, Thank you for correcting my mistake.
    SQL> edit
    Wrote file afiedt.buf
    1 CREATE TABLE placement
    2 (
    3 contactId Int,
    4 placementId Int,
    5 position VARCHAR(60),
    6 description CLOB,
    7 lengMonths Int,
    8 salary Number(7,2),
    9 deadline DATE check (deadline between sysdate and sysdate+365),
    10 startDate DATE,
    11 addrLine1 VARCHAR(120),
    12 postCd VARCHAR(10)
    13* )
    SQL> /
    deadline DATE check (deadline between sysdate and sysdate+365),
    ERROR at line 9:
    ORA-02436: date or system variable wrongly specified in CHECK constraint
    SQL> edit
    Wrote file afiedt.buf
    1 CREATE TABLE placement
    2 (
    3 contactId Int,
    4 placementId Int,
    5 position VARCHAR(60),
    6 description CLOB,
    7 lengMonths Int,
    8 salary Number(7,2),
    9 deadline DATE check (deadline between to_date('2007-03-21','yyyy-mm-dd') and to_date('2008-03-21','yyyy-mm-dd')
    10 startDate DATE,
    11 addrLine1 VARCHAR(120),
    12 postCd VARCHAR(10)
    13* )
    SQL> /
    Table created.
    On the contrary, I think that OP want an error instead of rollback : "I would like for these triggers to display an error rather than just using ROLLBACK"Ah, I had misread additionally. Sorry.

  • Creating Update Trigger

    Hello,
    I am pretty sure the problem here is ignorance. I am trying to create a trigger that will put the sysdate in a column called MODIFIED anytime the row is updated.
    create or replace trigger "INSP_ER_KEY_MOD"
    AFTER
    update on "INSP_ER_KEY"
    begin
    update INSP_ER_KEY set MODIFIED = sysdate;
    end;Thank you,
    Adam

    Hello
    What is missing from the code Tony posted is the 'co-relation' name for the column to be updated. By default this is ':new' for the new value of the column.
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm#sthref1228
    CREATE OR REPLACE TRIGGER INSP_ER_KEY_MOD
    BEFORE UPDATE
    ON INSP_ER_KEY
    FOR EACH ROW
    BEGIN
    -- Update updated_date field to current system date
    :new.Modified := sysdate;  --- changed
    END;varad

  • Help With creating a trigger

    Hello All!
    I am writing regarding a trigger I must create. I have a table that has roughly 10 columns. I must insert rows into a 'history' table when any column in the parent table is updated or, of course if there is an insert on the parent table. Is there a way to specify multiple columns in the triggering statement (i.e., UPDATE OF parts_on_hand ON inventory) to insert rows into the child table if any of the columns is updated? I am very new with triggers, and am hoping that someone might be able to offer any suggestions, or maybe sample code if available just to help start me out.
    Thanks in advance!
    Julie

    If you do not include a specific column(s), then the trigger will fire on an update to any column. So, for your case:
    create or replace trigger t_trigger
    before insert or update on t
    for each row
    begin
      insert into t_history values (:new.c1, :new.c2, ...);
    end;
    /

  • Help on Procedure and trigger for updating(urgent please)

    SQL> / Table A
    CTUT_ID CTUT_COMPANY_NAME CURRT_USER_ID FMIS_ID CREATE_DA UPDATE_BY UPDATE_DATE
    1234 A 15-APR-03
    2222 B 15-APR-03
    3333 C 15-APR-03
    4444 D 15-APR-03
    5555 E 15-APR-03
    6666 F 15-APR-03
    150282 G oRACLE 23-APR-03
    1 H 15-APR-03
    2 I 15-APR-03
    3 J 15-APR-03
    150343 K TIGER 24-APR-03
    150305 L EXAMPLE 23-APR-03
    150342 M SCOTT 24-APR-03
    sQL >/ Table B
    Empno     Empname UPDATE_BY UPDATE_DATE
    1 AA
    2 BB
    3 CC
    4 DD
    What i need to do is i need to create an update trigger on both tables
    like create a procedure
    1)In procedure i need to check like
    IF TABLEA.CURRT_USER_ID = (SELECT USER FROM DUAL)
    THEN
    UPDATE_BY = (CURRENT_USER_ID of CTUT_ID)
    FOR EXAMPLE CURRENT USER_ID IS SCOTT THEN
    UPDATE_BY = 150342
    UPDATE_DATE = SYSDATE
    ELSIF
    UPDATE_BY <=> (CURRENT_USER_ID of CTUT_ID)
    THEN
    MESSAGE('USER IS NOT IN TABLE);
    END IF;
    and call that procedure in the update triggers
    FOR BOTH TABLES TABLEA,TABLEB
    i CREATED A PROCEDURE BUT IT IS NOT WORKING
    ANY HELP PLEASE
    CREATE OR REPLACE PROCEDURE UPDATE(
    UPDATE_DATE out DATE,
    UPDATE_BY out VARCHAR2)
    IS
    Uuser varchar2(20);
    Udate date;
    Ufound number(1);
    BEGIN
    SELECT USER,SYSDATE
    INTO Uuser,Udate from dual;
    SELECT count(*),CTUT_ID into Ufound,Uctut_id
    FROM TABLEA
    WHERE CURRT_USER_ID = Uuser
    Group by Ctut_id;
    IF (UFOUND = 1) THEN
    UPDATE_DATE := UDATE;
    UPDATE_BY := UCTUT_ID;
    END IF;
    EXCEPTION WHEN NO_DATA_FOUND THEN
    RAISE_APPLICATION_ERROR(-20001,'User Does not Exist');
    END UPD_CONSTITUENT;
    CREATE A TRIGGER :
    CREATE OR REPLACE TRIGGER TU
    BEFORE INSERT ON TABLEA
    FOR EACH ROW
    BEGIN      
    UPDATE(:NEW.update_date,
         :NEW.update_BY);
    END IF;      
    END;
    SQL> update TABLEA
    2 set CTUT_COMPANY_NAME = 'SCOTT TEST'
    3 WHERE FMIS_USER_ID = 'N';
    update TABLEA
    ERROR at line 1:
    ORA-04091: table TABLEA is mutating, trigger/function may not see it
    ORA-06512: at "UPDATE", line 12
    ORA-06512: at "TU", line 1
    ORA-04088: error during execution of trigger 'TU'

    Hi Mara,
    You are right thats what i want
    I have a table A
    EmpNo Empname Currtuser_id Update_date Updateby
    1 Denis Oracle
    2 Scott Scott
    3 Mara MMara
    1)what i need to do is when any user tries to update the table Table A
    Then the Trigger or procedure should check whether user is exits in table A in column currtuser_id
    If his user id exits in table A
    Then allow him to update the TABLE A
    and insert his EMPNO in UPDATE_BY
    and SYSDATE in UPDATE_BY
    He will do all this process using forms
    But i need to have trigger or procedure in database level for table
    2) I have another table like 10 tables
    Suppose TABLE B
    When user tries to update TABLE B
    Then the Trigger or procedure should check whether user is exits in table A in column currtuser_id
    If his user id exits in table A
    Then allow him to update the TABLE B
    and insert his EMPNO in UPDATE_BY
    and SYSDATE in UPDATE_BY
    3) I need to have a common Procedure and call that procedure in all tables in UPDATE TRIGGER
    Thanks for your help
    Thanks

  • Help me in creating a Trigger for Insert and Update Options

    Hi
    Please help me in creating a Trigger .
    My requirement is that after insert or update on a Table , i want to fire an event .
    I have started this way ,but doesn't know how to fully implement this .
    say i have a dept table
    CREATE TRIGGER DepartmentTrigger
    AFTER INSERT ON Dept
    BEGIN
    INSERT INTO mytable VALUES("123","Kiran");
    END DepartmentTrigger;
    Please tell me how can i put the Update option also .
    Thanks in advance .

    Please tell me how can i put the Update option also .Add "Or Update". ;-)
    Here are a few suggestions, but you definitely need to refer to the manual page that the previous poster suggested.
    CREATE OR REPLACE TRIGGER DepartmentTrigger
    AFTER INSERT Or Update ON Dept
    BEGIN
    INSERT INTO mytable VALUES(:new.Dept,'DEPT ADDED OR CHANGED');
    END DepartmentTrigger;
    The "Or Replace" means you can replace the trigger while you're developing without having to type in a drop statement every time. Just change and rerun your script, over and over until you get it right.
    Adding "Or Update" or "Or Delete" makes the trigger fire for those events too. Note, you may want seperate triggers in different scripts and with different names for each event. You have to decide if your design really does the same thing whether it's an insert or an update.
    :new.Dept is how you would refer to the changed vale of the Dept column (:old.Dept is the prior value). I changed the double quotes on the string in the VALUES clause to single quotes.
    Andy

  • Need help in writin a trigger---Very Urgent

    Help me in creating the trigger with below specifications.
    "We want to reject record deletions where DATE_APPROVED_PROV is not null or NLA_PROV_DONE is not null
    We want to reject updates where (DATE_APPROVED_PROV is not null or NLA_PROV_DONE is not null)
    and
    (any of CAPEX or RFS or ALLOCATIONS or DEPR_START_DATE is different before update than it would be after update)"
    Defination of Table is below :
    CREATE TABLE ir_data
    (record_id NUMBER(12,0) NOT NULL,
    request_title VARCHAR2(255),
    cio VARCHAR2(30),
    host_country VARCHAR2(20),
    ben_project_name VARCHAR2(255),
    application VARCHAR2(80),
    req_summary VARCHAR2(500),
    external_ref VARCHAR2(255),
    submitted_by VARCHAR2(60),
    approved_by_prov VARCHAR2(60),
    date_approved_prov DATE,
    approved_by_final VARCHAR2(60),
    date_approved_final DATE,
    nla_prov_done DATE,
    nla_final_done DATE,
    cost_alloc_method VARCHAR2(255),
    capex NUMBER(12,0) DEFAULT 0,
    allocations NUMBER(12,0) DEFAULT 0,
    rfs NUMBER(12,0) DEFAULT 0,
    ben_budget_id VARCHAR2(20),
    delete_flag VARCHAR2(1),
    amortisation NUMBER(12,0) DEFAULT 0
    depr_start_date DATE,
    fy_allocations NUMBER(12,0),
    wusys VARCHAR2(1))

    Help me in creating the trigger with below
    specifications.
    "We want to reject record deletions where
    DATE_APPROVED_PROV is not null or NLA_PROV_DONE is
    not null
    We want to reject updates where (DATE_APPROVED_PROV
    is not null or NLA_PROV_DONE is not null)
    and
    (any of CAPEX or RFS or ALLOCATIONS or
    DEPR_START_DATE is different before update than it
    would be after update)"
    Defination of Table is below :
    CREATE TABLE ir_data
    (record_id NUMBER(12,0) NOT
    NULL,
    request_title VARCHAR2(255),
    cio VARCHAR2(30),
    host_country VARCHAR2(20),
    ben_project_name VARCHAR2(255),
    application VARCHAR2(80),
    req_summary VARCHAR2(500),
    external_ref VARCHAR2(255),
    submitted_by VARCHAR2(60),
    approved_by_prov VARCHAR2(60),
    date_approved_prov DATE,
    approved_by_final VARCHAR2(60),
    date_approved_final DATE,
    nla_prov_done DATE,
    nla_final_done DATE,
    cost_alloc_method VARCHAR2(255),
    capex NUMBER(12,0) DEFAULT
    0,
    allocations NUMBER(12,0)
    DEFAULT 0,
    rfs NUMBER(12,0)
    DEFAULT 0,
    ben_budget_id VARCHAR2(20),
    delete_flag VARCHAR2(1),
    amortisation NUMBER(12,0)
    DEFAULT 0
    depr_start_date DATE,
    fy_allocations NUMBER(12,0),
    wusys VARCHAR2(1))(any of CAPEX or RFS or ALLOCATIONS or DEPR_START_DATE is different before update than it would be after update)
    I think if any of these change during an update do something. Not sure what. Probably reject, because that is what the other 2 requirements are.
    Is this for an homework assignment?
    Jim P.

  • Need help with create trigger based on more then 1 table and join.

    Hello,
    Here i have 3 tables
    1. Employee
    PERSON_ID
    1
    1
    N
    NUMBER
    None
    ORG_ID
    2
    N
    NUMBER
    Frequency
    LOC_ID
    3
    N
    NUMBER
    Frequency
    JOB_ID
    4
    Y
    NUMBER
    Height Balanced
    FLSA_STATUS_ID
    5
    Y
    NUMBER
    Frequency
    FULL_NAME
    6
    N
    VARCHAR2 (250 Byte)
    Height Balanced
    FIRST_NAME
    7
    N
    VARCHAR2 (20 Byte)
    Height Balanced
    MIDDLE_NAME
    8
    Y
    VARCHAR2 (60 Byte)
    Height Balanced
    LAST_NAME
    9
    N
    VARCHAR2 (40 Byte)
    Height Balanced
    PREFERRED_NAME
    10
    Y
    VARCHAR2 (80 Byte)
    None
    EMAIL
    11
    Y
    VARCHAR2 (250 Byte)
    None
    MAILSTOP
    12
    Y
    VARCHAR2 (100 Byte)
    None
    HIRE_DATE
    13
    N
    DATE
    None
    2. ems_candidate
    EMS_CANDIDATE_ID
    1
    1
    N
    NUMBER
    None
    EMS_JOB_ID
    2
    Y
    NUMBER
    Frequency
    NAME
    3
    N
    VARCHAR2 (255 Byte)
    Frequency
    EMAIL
    4
    Y
    VARCHAR2 (255 Byte)
    None
    TELEPHONE
    5
    Y
    VARCHAR2 (25 Byte)
    None
    EMS_SOURCE_ID
    6
    Y
    NUMBER
    Frequency
    RECEIVED_DATE
    7
    Y
    DATE
    Frequency
    COMMENTS
    8
    Y
    VARCHAR2 (4000 Byte)
    None
    3. employee_resources
    EMP_RES_ID
    1
    1
    N
    NUMBER
    None
    PERSON_ID
    2
    Y
    NUMBER
    Height Balanced
    CANDIDATE_ID
    3
    Y
    NUMBER
    Frequency
    EMP_START_DATE
    4
    Y
    DATE
    None
    CUSTOM_RESOURCE_FLAG
    5
    Y
    NUMBER (1)
    None
    RESOURCE_GROUP_ID
    6
    N
    NUMBER
    Frequency
    RESOURCE_STATUS_ID
    7
    N
    NUMBER
    Frequency
    GROUP_LOC_ID
    8
    N
    NUMBER
    Height Balanced
    ASSIGNED_JIRA
    9
    Y
    VARCHAR2 (250 Byte)
    None
    REVOKED_JIRA
    10
    Y
    VARCHAR2 (250 Byte)
    None
    CREATED_DATE
    11
    Y
    DATE
    SYSDATE
    None
    UPDATED_DATE
    12
    Y
    DATE
    None
    Now i want to create trigger when new record get inserted in employee table wanted to update person_id in employee_resources table.
    So i want to match ems_candidate.name with employee.full_name , ems_candidate.ems_job_id with employee.ems_job_id. And if it matched then update person_id in employee_resources table else through an exception and insert record in temp table.
    If anybody has an idea can u please help me.
    Thanks,
    Gayatri.

    I created below trigger
    CREATE TRIGGER emp_resources_upd_person_id
    AFTER INSERT ON ems.employee
    FOR EACH ROW
    BEGIN
        UPDATE ems.employee_resources
           SET person_id = :new.person_id
         WHERE candidate_id = (SELECT ems_candidate_id  
                                 FROM ems.ems_candidate cand, ems.employee emp
                                WHERE TRIM(UPPER(emp.first_name)) = TRIM(UPPER(SUBSTR (cand.name, 1, INSTR (cand.name, ' ') - 1)))
                                  AND TRIM(UPPER(emp.last_name)) = TRIM(UPPER(SUBSTR (cand.name,INSTR (cand.name, ' ') + 1,DECODE (INSTR (SUBSTR (cand.name, INSTR (cand.name, ' ') + 1), ' '),0,LENGTH (cand.name),(INSTR (SUBSTR (cand.name, INSTR (cand.name, ' ') + 1), ' ') - 1)))))
                                  AND emp.person_id = :new.person_id);
    EXCEPTION
      WHEN OTHERS THEN
        INSERT INTO ems.update_person_id_exception(person_id,first_name,last_name,full_name) VALUES(:new.person_id,:new.first_name,:new.last_name,:new.full_name);
    END;
    Now when i am trying to insert row in ems.employee  table it gives me an error
    ORA-04091
    table string.string is mutating, trigger/function may not see it
    Cause: A trigger (or a user defined plsql function that is referenced in this statement) attempted to look at (or modify) a table that was in the middle of being modified by the statement which fired it.
    Action: Rewrite the trigger (or function) so it does not read that table.
    Can anybody please help me to come out from these error.
    Thanks,
    Gayatri.

  • How to create conditional update trigger in sql server

    How to create conditional update trigger in sql server

    You cant create a conditional update trigger. Once you create an update trigger it will get called for every update action. However you could write logic inside it to make it do your activity based on your condition using IF condition statement
    Say for example if you've table with 6 columns and you want some logic to be implemented on update trigger only if col3 and col5 are participating in update operation you can write trigger like this
    CREATE TRIGGER Trg_TableName_Upd
    ON TableName
    FOR UPDATE
    AS
    BEGIN
    IF UPDATE(Col3) OR UPDATE (Col5)
    BEGIN
    ....your actual logic here
    END
    END
    UPDATE() function will check if column was involved in update operation and returns a boolean result
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Learning Pl/SQL here: Help creating a trigger

    Hello everyone.
    I am just learning PL/SQL here and have tasked with creating at trigger. The trigger basically needs to fire off a email when any records are updated on a specific table.
    So far, I was going to use the built in procedure utl_smtp. I have that part configured, but where I am struggly is how to tell the trigger work when the table is updated with new data.
    I am a little lost here so any help is greatly appreciated.
    Thanks,
    JW

    Hello! I have following code for a trigger that fires when some record inserted in the table. However I got following error. Like this for example "ERROR at line 38: PL/SQL: Statement ignored". What is wrong whith the code?
    CREATE OR REPLACE TRIGGER "SPEAKERS_T2"
    AFTER
    insert on "SPEAKERS"
    for each row
    declare
    sender varchar2(80) := '[email protected]';
    recipient varchar2(80) := '[email protected]';
    smtpHost varchar2(80) := 'smtp.hotmail.com';
    smtpUser varchar2(256) := 'someusername';
    smtpPassword varchar2(256) := '<password>';
    mail_conn utl_smtp.connection;
    separator varchar2(1) := ' ';
    content_charset varchar2(8) := 'utf8';
    procedure send_header(name in varchar2, header in varchar2) as
    begin
    utl_smtp.write_raw_data(mail_conn, UTL_RAW.cast_to_raw(name||': '||header||utl_tcp.CRLF));
    end;
    begin
    mail_conn := utl_smtp.open_connection(smtpHost, 25);
    utl_smtp.helo(mail_conn, smtpHost);
    -- If Authentication required for SMTP
    -- After upgrade to Oracle 9i, replace demo_base64.encode
    -- with the native utl_encode.base64_encode for better performance.
    utl_smtp.command(mail_conn, 'AUTH LOGIN');
    utl_smtp.command(
    mail_conn, utl_raw.cast_to_varchar2(
    utl_encode.base64_encode(utl_raw.cast_to_raw(smtpUser))
    utl_smtp.command(
    mail_conn, utl_raw.cast_to_varchar2(
    utl_encode.base64_encode(utl_raw.cast_to_raw(smtpPassword))
    utl_smtp.mail(mail_conn, sender);
    utl_smtp.rcpt(mail_conn, recipient);
    utl_smtp.open_data(mail_conn);
    send_header('From', sender);
    send_header('To', recipient);
    send_header('CC', recipient);
    send_header('Subject', 'Test');
    send_header('MIME-Version', '1.0');
    send_header('Content-Type', 'text/plain; charset="utf8"');
    utl_smtp.write_raw_data(
    mail_conn, utl_raw.cast_to_raw(utl_tcp.CRLF||' ')
    utl_smtp.close_data(mail_conn);
    utl_smtp.quit(mail_conn);
    end;

  • Regarding before insert and update trigger...Urgent!!

    i have written a trigger to trim N upper the 1st field and trim N upper N replace period(.) for second field2 for future records.
    When i compile, it says "complied with errors" is gives error as "insuffecient privlages"
    Can anyone please figure out what problem is this??? And is my code correct?? if any changes to be done please let me know????
    create or replace trigger TRG_test
    before insert or update on test
    for each row
    declare
    begin
    :new.field1 := trim(upper(:new.field1));
    :new.field2 := trim(upper(replace(:new.field2,'.' )));
    end;
    Urgent please!!!
    Thank you in advance.

    The owner of the tables is User 2 and i am creating the trigger in user1 N i m calling this table from user2...as User2.TEST ...
    User 1 as has previliges to acess the user 2 tables, since user2 tables are listed under user1 table section...
    Do you system privilages????
    I m using PL/SQL devloper tool....

  • ADF BC: Creating updatable VO based upon DB View with "instead of" trigger

    Hello all,
    I have got an interesting issue. I have an Oracle DB view that is used to hide some complexity in the underlying DB design (it does some unions). This view is updatable because we have created an "instead of" update trigger to update the correct table when a row is updated. This is working fine in SQL.
    Next, we have created an ADF Entity object based upon the view, specifying an appropriate PK for the DB View. Then, we have created an updatable VO based upon the EO. All well and good so far. The issue we have is in trying to commit changes to the DB - because the ADF BC framework is trying to lock the row to update (using SELECT ... FOR UPDATE), it's not working because of ORA-02014 - cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc.
    This leads me to thinking about overridding doSelect() on the EO as hinted here http://radio.weblogs.com/0118231/stories/2005/07/28/differenceBetweenViewObjectSelectAndEntityDoselectMethod.html
    As a temporary test, we have over-ridden the EO's doSelect to call super.doSelect(false) and it does work, although we will have lost update issues as detailed in Steve's article.
    My questions:
    1). Is overriding doSelect() the correct thing here? Perhaps there is a better way of handling this problem? I do have a base EO class from which all of the EO's extend, so adding this behavior should be straightforward.
    2). Does anyone have example doSelect implementation? I am thinking of overriding doSelect for my EO and calling super.doSelect (lock=false), but then I need to deal with some possible exceptions, no?
    Kind regards,
    John

    Hi John,
    I have exactly the same issue as you experienced back in January. I have a complex data modelling requirement which requires the need to pivot rows into columns using ROW_NUMBER() and PARTITION clauses. To hide the complexity from the middle tier, I have created a database view and appropriate INSTEAD OF triggers and mapped my EO to the view. I have overriden the lock() method on the EO implementation class (to avoid ORA-02014) and would like to try the same solution you used with the pl/sql call to lock the record.
    My question is, how did you manage the release of the lock if the transaction was not rolled back or committed by your application i.e. if the user closed the browser for instance.
    In my naivity, I would like to think that the BC4J framework would release any locks for the database session when it found the servlet session to be terminated however my concern is that the lock would persist and cause complications.
    Any assistance greatly appreciated (if you would be willing to supply your lock() method and pl/sql procedure logic I would be even more grateful!).
    Many thanks,
    Dave
    London

  • Help needed in creating a trigger

    hi,
    I am creating a Trigger which keep track of the updates of a particular table.
    Whenver a col value is updated i want to insert a row into newtable, with colname,old value and new value.
    How do we get to know which col value is changed in a table?
    Thanks

    I have seen the approach you propose (1 row for each column changed) and it is tedious and prone to maintenance headaches. You have to write code for each column, doing compares (remembering to consider NULLs), dealing with different datatypes, etc. You also have to decide how to record inserts and deletes.
    An easier trigger-based solution is to create a history table for the table you want to track, with all of the columns from the original, plus whatever else you need for housekeeping. Write an After Insert/Update/Delete trigger on your base table, and populate/insert rows into the history table:
    - For inserts, populate and insert row from the :new. values
    - For deletes, populate and insert a row from the :old. values
    - For updates, popualte and insert a row from the :old. values and another from the :new. values
    I would also have a column to designate whether this is an Insert, Delete, Update-Old or Update-New row.
    Once you have done one example, the rest are easy. If you were sufficiently motivated (I have not yet been), you could probably write a script to query DBA_TAB_COLS and generate the code.

Maybe you are looking for