trigger for insertion of a table to another table

Hello again,

I tried to write a trigger after insert or update will happen in table 1, it will move some values in another table, as shown below:

Create or replace trigger Insert_Amount_Credit after insert or update on Reciept_Voucher

Referencing NEW AS NEW OLD OLD FOR EACH LINE AS
Start

Insert into customer_Details (Transaction_Date, client_name, Description, credit) values (new. Rec_Date, new. Client_name, CONCAT (' receipt number:', again.) Rec_ID), new. Amount);

End;

/

The trigger was created but with compilation error... and my question is:

1. How do I write the code above in the best way?

2. How can I enough the other table with a table concatenated as what I was trying to make above?

any idea?

Something like that?

CREATE OR REPLACE TRIGGER trig_ins_testtrg2

BEFORE INSERT OR UPDATE ON test_trg1

FOR EACH LINE

BEGIN

INSERT INTO test_trg2 (curr_date,

job_id,

reason,

credit)

VALUES (SYSDATE,

: NEW.id,.

: NEW.receipt_no |:NEW.description,.

(: NEW.amount);

END;

Tags: Database

Similar Questions

  • How can I write the trigger for the global temporary Table

    Hi Grus,
    How can I write the trigger for the global temporary Table.

    I created the TWG with trigger using the script below.


    CREATE A GLOBAL_TEMP GLOBAL TEMPORARY TABLE
    (
    EMP_C_NAME VARCHAR2 (20 BYTE)
    )
    ON COMMIT PRESERVE ROWS;


    CREATE OR REPLACE TRIGGER TRI_GLOBAL_TEMP
    BEFORE DELETE, UPDATE OR INSERT
    ON GLOBAL_TEMP
    REFERRING AGAIN AS NINE OLD AND OLD
    FOR EACH LINE
    BEGIN
    INSERT INTO VALUES EMPNAME (: OLD.) EMP_C_NAME);
    END;
    /


    trigger was created successfully, but her would not insert EMPNAME Table...

    Please guide if mistaken or not? If not wanting to give a correct syntax with example


    Thanks in advance,
    Arun M M
    BEGIN
    INSERT INTO EMPNAME VALUES (:OLD.EMP_C_NAME);
    END;
    
    you are referencing old value in insert stmt.
    
    BEGIN
    INSERT INTO EMPNAME VALUES (:new.EMP_C_NAME);
    END;
    

    then run your app, it works very well...

    CREATE GLOBAL TEMPORARY TABLE GLOBAL_TEMP
    (
    EMP_C_NAME VARCHAR2(20 BYTE)
    )
    ON COMMIT PRESERVE ROWS;
    
    CREATE OR REPLACE TRIGGER TRI_GLOBAL_TEMP
    BEFORE DELETE OR UPDATE OR INSERT
    ON GLOBAL_TEMP
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    BEGIN
    dbms_output.put_line(:OLD.EMP_C_NAME||'yahoo');
    INSERT INTO EMPNAME VALUES (:new.EMP_C_NAME);
    dbms_output.put_line(:OLD.EMP_C_NAME);
    END;
    /
    
    create table EMPNAME as select * from GLOBAL_TEMP where 1=2
    
    insert into GLOBAL_TEMP values('fgfdgd');
    commit;
    select * from GLOBAL_TEMP;
    select * from EMPNAME;
    
    output:
    1 rows inserted
    commit succeeded.
    EMP_C_NAME
    --------------------
    fgfdgd               
    
    1 rows selected
    
    EMP_C_NAME
    --------------------
    fgfdgd               
    
    1 rows selected
    

    He got Arun

    Published by: OraclePLSQL on December 28, 2010 18:07

  • FRM-40735: trigger for INSERTION after raised unhandled exception ORA-25207

    Hello
    I am trying to create custom applications. In that when creating custom responsibility I get the following error
    FRM-40735: trigger for INSERTION after raised unhandled exception ORA-25207

    I know ORA-25207 is related to the activation of the queues, but how do I find out which queue this error is ponting at.

    File alertlog Checked, $APPLCSF / $APPLLOG found nothing.

    Any help is appreciated

    Concerning
    Taher

    PL see if the steps described in MOS Doc 211095.1 (11, 5 - AP: Frm-40735: trigger of sur-Commit not triggered managed Ora - 25207 Exception on APXPAWKB) can help.

    It is an odd error and should not occur when you create custom responsibilities. You have customizations/triggers/customizations enabled on the form of accountability?

    HTH
    Srini

  • Create a button for inserting into a table

    What I can't do, because I've never needed, is to create, in a page (where I have an interactive report), a button is clicked that once performs a simple sql (for example, an insert into a table using the values of page element).

    How can I do?

    Thank you

    Here are some of the steps:

    (1) create the button.

    (2) then right click on the name of the button and select Create Dynamic Action

    (3) give it to DA a name, click Next

    (4) on the 'when' step, event should already be "Click" and the name of the button must be filled

    (5) for the Condition, you would have a condition when you want that the display of the button.  Say, if your page elements have a certain range of values, and then display this button.  No condition means that the button is always displayed.

    (6) for the 'real Action' stage, action, select "run the Code in PL/SQL.

    [There are other ways to do this, but it's simple for me.]

    (7) in the Code PL/SQL block, enter says:

    Begin

    INSERT INTO EMP (EMPNO, ENAME, HIREDATE)

    VALUES (: P6_EMPNO, 'MARK1970',: P6_HIREDATE);

    COMMIT;

    END;   -You'd probably all the variables page element

    (8) here the part I'm not sure 100%.

    For items to submit "Page, name used in YOUR QUERY - in my example page elements, these are P6_EMPNO, P6_HIREDATE (don't note any use of & or: or here.)

    Hmm.  Start by putting nothing in "Page of elements to return.   If that fails, then try the same elements of page P6_EMPNO, it P6_HIREDATE as well.

    [I'm too lazy to go running at present].

    (9) I think that's it.

    Howard

  • Trigger Oracle (INSERT ONLY) question (Table of historical data)

    What I have to do is follow the changes that are taking place on a table. The requirement is that it must be a trigger. The question I have is that a MERGE statement is moving data to another table that I monitor, which creates tons of changes (which are really just duplicates).

    The underside of work... with the exception of INSERTING it. When a change or remove met him, archives... but not INSERT it. I noticed that when I put where when to '=' instead of '<>', INSERTS will be saved (but obviously when new old =, which I don't want). The problem seems to be the "WHEN".

    Any suggestions...?

    CREATE OR REPLACE TRIGGER SCHEME. TRIGGERTEST22
    After you INSERT or DELETE or update T_RESOURCE_ENTITY
    THE ONTARIO SCHEME. TABLE
    FOR EACH LINE
    WHEN (nvl (new. T_RESOURCE_ENTITY, 'YYY') <>NVL (former. T_RESOURCE_ENTITY, "XXX"))
    BEGIN
    IF (VALUES) THEN
    INSERT IN THE SCHEMA. TABLE_HISTORY2 (ARCHIVED_BY, ARCHIVED_DATE, ARCHIVED_TYPE, COLUMN_NAME, COLUMN_VALUE_NEW, COLUMN_VALUE_OLD)
    VALUES ('USERNAME', SYSDATE, 'IN',: new.) T_RESOURCE_TYPE,: new. T_RESOURCE_ENTITY,: old. T_RESOURCE_ENTITY);
    ELSIF (UPDATE) THEN
    INSERT IN THE SCHEMA. TABLE_HISTORY2 (ARCHIVED_BY, ARCHIVED_DATE, ARCHIVED_TYPE, COLUMN_NAME, COLUMN_VALUE_NEW, COLUMN_VALUE_OLD)
    VALUES ('USERNAME', SYSDATE, 'UP',: new.) T_RESOURCE_TYPE,: new. T_RESOURCE_ENTITY,: old. T_RESOURCE_ENTITY);
    ELSIF (DELETING) THEN
    INSERT IN THE SCHEMA. TABLE_HISTORY2 (ARCHIVED_BY, ARCHIVED_DATE, ARCHIVED_TYPE, COLUMN_NAME, COLUMN_VALUE_NEW, COLUMN_VALUE_OLD)
    VALUES ('USERNAME', SYSDATE, 'DEL',: new.) T_RESOURCE_TYPE,: new. T_RESOURCE_ENTITY,: old. T_RESOURCE_ENTITY);
    END IF;
    END;

    : old is NOT available to INSERT.

    This means a trigger will not be enough.

    -----------
    Sybrand Bakker
    Senior Oracle DBA

  • Trigger for INSERTION OF THE COLUMN exists?

    Hello Experts

    I just wonder that relaxation of the COLUMN INSERT exists as UPDATE OF THE COLUMN? If so, it seems illogical to use insert the trigger. Because can not insert for a spesific columns that we effect the registration worldwide, right line?

    Thank you

    PRIOR TO INSERTION OF THE MEMBER ON AVLMEMBERS ID

    Don't have compile this code of yours? View the results of the compilation of this trigger.

    There is NO such syntax as a review of the language PL/SQL doc will show you

    CREATE TRIGGER statement

    dml_event_clause

    Specifies the trigger instructions for simple_dml_trigger or compound_dml_trigger . The database enables the trigger in the existing user transaction.

    DELETE

    Causes the database to activate every time a DELETE statement drops a line to table or table on which view is defined.

    INSERT

    Causes the database to activate every time a INSERT statement adds a line to table or table on which view is defined.

    UPDATE [columns [, column]]

    Causes the database to activate every time a UPDATE statement changes a value in a specified column. Default: the current database the trigger every time that a UPDATE statement changes a value in a column of table or table on which view is defined.

    If you specify a column , then you cannot change its value in the body of the trigger.

  • How to write the trigger for update of 2 tables

    Hi all
    I have 2 tables,

    1.lv_data,
    It contains the following fields,
    emp_name        tot_days
     guru                    18
                
    2.leave_data
    It contains the following fields,
     emp_name          From_date              to_date            no_of_days       remaining_days
     guru                    02/05/2012          03/05/2012           2                         
    In the second table, if the data is inserted.
    the no_of_days will be calculated automatically (from to_date - From_date) + 1,

    Here, I need to write the trigger to update the remaining_days column,

    First table for all tot_days emp_name is 18 days,
    Thus, in the second table every time that the record is inserted.
    the remaining_days must be calculated as follows,

    remaining_days: = tot_days - no_of_days and this (calculated) value should be updated in the tot_days column in the first table (lv_data).

    Example:
    emp_name        tot_days
    guru                    18
                
    2.leave_data
     emp_name          From_date              to_date            no_of_days        remaining_days
     guru                    02/05/2012          03/05/2012         2                          16
    [/code  
    
    Now the first table should be updated like,
    emp_name tot_days
    Guru 16


    So I need to update to 2 tables,
    can someone help me to update these 2 tables through TRIGGER?

    Thank you
    Kind regards
    gurujothi

    Edited by: Gurujothi may 1, 2012 19:54

    It is generally considered bad practice to store the value even in 2 places. You are having the kind of problems that you are experiencing now. It is much easier to store in the main table and get the value from there where you need to show in your forms.

  • Trigger for insertion post

    Hi all

    I want to update a column on the newly inserted row. So I wrote a trigger on the table, but it does not update the current row. For example

    If there were two rows of the table with the same criteria, two were created a minute ago, trigger will update the two lines but not the current.

    No idea how to upgrade the new line. I think that AFTER insertion, row trigger should be available in the table and must be updated, but is not.

    Thank you

    John

    user12294561 wrote:

    I want to update a column on the newly inserted row. So I wrote a trigger on the table, but it does not update the current row. For example

    Etc...

    You need a BEFORE INSERT... FOR EACH type of trigger.
    : p

  • Create the trigger after insert

    Hello

    I would like to ask how to create a trigger after insert on a table. Basically, what I wanted to do are when an insert to table1 it will insert the records to table2. But the problem is that folders that will be inserted in the new tables are entries/fields, who lived in NEW York City (on table of ld)

    after insertion, I wanted to add another field in table2 as status


    create table table1)

    Name varchar2 (55),
    City varchar2 (55)

    );

    create (table2)

    Name varchar2 (55)
    status int (1)
    )

    Hope you could help me.

    Thank you

    Best regards

    antok1015 wrote:
    Hello

    I would like to ask how to create a trigger after insert on a table. Basically, what I wanted to do are when an insert to table1 it will insert the records to table2.

    It's easy...

    SQL> create table table1(
      2    Name varchar2(55),
      3    City varchar2(55)
      4  );
    
    Table created.
    
    SQL>
    SQL> create table table2 (
      2    Name varchar2(55),
      3    status number(1)
      4  );
    
    Table created.
    
    SQL>
    SQL> create or replace trigger trg_tbl1 after insert on table1
      2  for each row
      3  begin
      4    insert into table2 values (:new.name, 1);
      5  end;
      6  /
    
    Trigger created.
    
    SQL> insert into table1 values ('Fred','Bob');
    
    1 row created.
    
    SQL> select * from table2;
    
    NAME                                                        STATUS
    ------------------------------------------------------- ----------
    Fred                                                             1
    
    SQL>
    

    But the problem is that folders that will be inserted in the new tables are entries/fields, who lived in NEW York City (on table of ld)

    after insertion, I wanted to add another field in table2 as status

    This is not sensible. Please explain what you mean.

  • trigger for update field in a table with the sum of the fields to another table

    My experience in creation of triggers and pl/sql in general can be described in terms of oracle with the null value. I practiced by creating arrays and applications on my personal server at home to help me with some of my work related tasks. Right now I am creating a trigger which will, after insert, update, delete on the update of the table assignment_time_track the area of the time_spent on the table of assignments with the sum of the time_spent on the assignment_time_track table fields. I hope that run on the sentence it is clear to the people other than me. I tried this script on my own using the tool of creation of trigger for Oracle Database Express Edition, but I get the following error:

    Create a trigger failed, for the following reason:
    ORA-06552: PL/SQL: ORA-06553 finished Compilation unit analysis: PLS-320: the declaration of the type of the expression is incomplete or incorrect

    Here is my attempt to create the trigger on my own.

    create or replace trigger "ASSIGNMENT_TIME_TRACK_T1".
    AFTER
    INSERT or update or delete on 'ASSIGNMENT_TIME_TRACK '.
    for each line
    Start
    update assignments
    Set time_spent = (select sum (time_spent)
    of assignment_time_track
    where assignment_time_track.name = assignments.name);

    end;
    /


    If what I posted is not clear or more detail is needed, let me know and I will respond with a full description of tables and my goals for each table. Thanks in advance for any help. I also gladly accepts links to tutorials or lessons that explain how to do this kind of thing.

    Published by: bobonthenet on March 9, 2009 14:01

    I think I understand what you mean :)

    Rather than use a trigger to keep the master table (assignments) in sync with the time spent on the details, it would be much easier to use a query to do this, maybe creating a view.

    Something along the lines of

    SQL> create table assignments
      2  (id number primary key
      3  ,name varchar2(10)
      4  );
    
    Table created.
    
    SQL>
    SQL> create table assignment_time_tbl
      2  (id number primary key
      3  ,assid number references assignments
      4  ,time_spent number
      5  );
    
    Table created.
    
    SQL>
    SQL> insert into assignments
      2  select rownum
      3       , 'a'
      4    from all_objects
      5   where rownum < 5
      6  /
    
    4 rows created.
    
    SQL>
    SQL> insert into assignment_time_tbl
      2  select rownum
      3       , rownum
      4       , rownum * 3
      5    from all_objects
      6   where rownum < 5
      7  /
    
    4 rows created.
    
    SQL>
    SQL> commit;
    
    Commit complete.
    
    SQL>
    SQL>
    SQL> select a.id
      2       , a.name
      3       , (select sum (time_spent)
      4            from assignment_time_tbl
      5           where assid = a.id
      6         )
      7    from assignments a
      8  /
    
            ID NAME       (SELECTSUM(TIME_SPENT)FROMASSIGNMENT_TIME_TBLWHEREASSID=A.ID)
    ---------- ---------- -------------------------------------------------------------
             1 a                                                                      3
             2 a                                                                      6
             3 a                                                                      9
             4 a                                                                     12
    
  • How to write the trigger for update or delete multiple columns in a table?

    Hello

    I create one in the form of table of sample_emp. In that, every time I want to change of name, team_id, team_leader_id, supervisor_id, manager_id it must store the update-able and old values of those in the job_history table. When I write the trigger for which it shows "ORA-04082 new or the old value not table level triggers. Here is my emp_table. My table also similar job_history like this. Need your help.

    Header 1 Header 2 Header 3 Header 4 Header 5 Heading 6 Heading 7 8 header Header 9
    EMP_IDEMP_NAMEDESIGNATIONTEAM_IDTEAM_LEADER_IDEMPLOYEEMANAGER_IDHIRE_DATERELIEVED_DATE

    --

    Thank you.

    Hi Joel,.

    Venky_prs wrote:

    Hello

    I create one in the form of table of sample_emp. In that, every time I want to change of name, team_id, team_leader_id, supervisor_id, manager_id it must store the update-able and old values of those in the job_history table. When I write the trigger for which it shows "ORA-04082 new or the old value not table level triggers. Here is my emp_table. My table also similar job_history like this. Need your help.

    Header 1 Header 2 Header 3 Header 4 Header 5 Heading 6 Heading 7 8 header Header 9
    EMP_ID EMP_NAME DESIGNATION TEAM_ID TEAM_LEADER_ID EMPLOYEE MANAGER_ID HIRE_DATE RELIEVED_DATE

    --

    Thank you.

    You can try something like this given below to complete the historical table on update and delete.

    create or replace TRIGGER  "CLONE_EMP_UPDATE_DELETE"
    BEFORE DELETE OR UPDATE ON EMP
    FOR EACH ROW
    DECLARE
        PRAGMA autonomous_transaction;
    BEGIN
    INSERT INTO JOB_HISTORY("EMP_ID","EMP_NAME","DESIGNATION","TEAM_ID","TEAM_LEADER_ID","SUPERVISOR_ID","MANAGER_ID"
    ,"HIRE_DATE","RELIEVED_DATE")
      VALUES(:OLD.EMP_ID,:OLD.EMP_NAME,:OLD.DESIGNATION,:OLD.TEAM_ID,:OLD.TEAM_LEADER_ID,:OLD.SUPERVISOR_ID,
    :OLD.MANAGER_ID,:OLD.HIRE_DATE,
    :OLD.RELIEVED_DATE);
    COMMIT;
    END;
    

    Hope this helps you,

    Kind regards

    Jitendra

  • How to identify the name of the procedure that inserts into a table in the TRIGGER

    Hello
    There are several procedures in my DB that perform the INSERT operation on a main table - MYTAB say.

    I created a trigger on the table MYTAB, to follow closely, what procedure is insert data in there. Is there a function that captures the name of this object?

    For example: PROC_A, PROC_B, PROC_C insert into the table MYTAB. And my query is - the trigger on table MYTAB should document these object names where the lines fit.
    Please let me know your suggestions...

    SQL> create table t (no integer, dt timestamp, who_inserted varchar2(4000));
    
    Table created.
    
    SQL> create or replace procedure p
      2  as
      3  begin
      4    insert into t (no, dt) values (1, systimestamp);
      5  end;
      6  /
    
    Procedure created.
    
    SQL> show err
    No errors.
    
    SQL> create or replace trigger t_trig before insert on t for each row
      2  begin
      3     :new.who_inserted := dbms_utility.format_call_stack;
      4  end;
      5  /
    
    Trigger created.
    
    SQL> show err
    No errors.
    
    SQL> begin
      2    p;
      3  end;
      4  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from t;
    
            NO DT                             WHO_INSERTED
    ---------- ------------------------------ ----------------------------------------
             1 31-OCT-14 04.58.53.668465 AM   ----- PL/SQL Call Stack -----
                                                object      line  object
                                                handle    number  name
                                              3a7ddbea0         2  KARTHICK.T_TRIG
                                              3ac979f30         4  procedure KARTHICK.P
                                              3a822deb8         2  anonymous block
    
  • Need a logic sql to insert from one table to another

    I have a table ( TABLEA ) as follows:

    type_id level

    -------------

    1       7

    2       4

    I would like to insert above values in another table ( TABLEB ) through DML so it looks like so:

    seq_id type_id name    order level

    -----------------------------------

    1     1       display  1     7

    2     1       header   2    

    3     1       detail   3    

    4     2       display  1     4

    5          2          header    2

    6         2           detail      3   

    Looks like a very complex sql is necessary, for which I have no knowledge of :-).

    Hello

    97a5a09d-76e1-4fc7-8113-80b4705be7f4 wrote:

    I have a table ( TABLEA ) as follows:

    type_id level 
    -------------
    
    1       7 
    2       4 
    

    I would like to insert above values in another table ( TABLEB ) through DML so it looks like so:

    seq_id type_id name    order level 
    -----------------------------------
    
    1     1       display  1     7 
    2     1       header   2     
    3     1       detail   3
    4     2       display  1     4
    5          2          header    2
    6         2           detail      3    
    

    Looks like a very complex sql is necessary, for which I have no knowledge of :-).

    So, for each line in tablea, you want to INSERT 3 rows in tableb, is that at the time?

    One way to do that is to tablea to join of a table (or the result set) with 3 rows, like cntr below:

    INSERT INTO tableb (seq_id type_id, row_name, row_order, lvl)

    WITH cntr AS

    (

    SELECT LEVEL AS row_order

    OF the double

    CONNECT BY LEVEL<=>

    )

    SELECT ROW_NUMBER () OVER (ORDER BY a.type_id

    c.row_order

    ) - or sequence_name. NEXTVAL

    a.type_id

    Case c.row_order

    WHEN 1 THEN 'display '.

    WHEN 2 THEN 'head '.

    WHEN 3 THEN «details»

    END

    c.row_order

    Case c.row_order

    WHEN 1 THEN a.lvl

    END

    FROM tablea a

    CROSS JOIN cntr c

    ;

    If you would care to post CREATE TABLE and INSERT for your tables (as it exists before the UPDATE), then I could test this.

    NAME is not a very good column name, but it is nowhere near as bad as the ORDER or LEVEL.  I called ROW_NAME, ROW_ORDER and LVL columns instead.

  • trigger for the insert statement

    Hello.
    I have a table, say, with 3 columns: ID, NAME, NAME_LENGTH.
    I am trying to create a trigger that fires when a record is inserted in the table.
    I send values for the ID and NAME columns per INSERT statement. and I need the trigger to write the LENGTH OF THE NAME INSERTED in the 3rd column

    CREATE OR REPLACE TRIGGER schema_name. GENERATE_length
    AFTER INSERT ON table_name
    FOR EACH LINE
    DECLARE
    F NVARCHAR2 (200);
    L NUMBER (5);
    NL NUMBER (5);
    NUMBER OF ID_LENGTH (5): = 5;
    BEGIN
    F: =:New.Name;

    L: = LENGTH (F);

    UPDATE HR_ORG_TREE

    SET name_length = NL;

    END;


    but does not work. error occurres as...

    ORA-04091: table schema_name.table_name is changing, the function of triggering/can not see
    ORA-06512: at the 'schema_name '. GENERATE_length', line 13
    ORA-04088: error during execution of trigger ' schema_name. GENERATE_length'

    Thank you.
    Sara
    CREATE OR REPLACE TRIGGER schema_name.GENERATE_length
        -- is "table_name" real name of your table?
        AFTER INSERT ON table_name
        FOR EACH ROW
    BEGIN
        -- this update will change all rows in HR_ORG_TREE table
        -- specify WHERE for this update
        UPDATE HR_ORG_TREE
           SET name_length = LENGTH(:NEW.name);
    END;
    

    Suppose your table_name is HR_ORG_TREE and you need to calculate the length of the name for just inserted row
    After maybe the code will be useful (not tested)

    CREATE OR REPLACE TRIGGER schema_name.GENERATE_length
        BEFORE INSERT ON HR_ORG_TREE
        FOR EACH ROW
    BEGIN
        :new.name_length := LENGTH(:NEW.name);
    END;
    

    Good luck

  • How can simple trigger for verification of status update on the table... - I get upd sql then?

    Hi guys,.

    I need to do a temporary simple audit on all updates on a table.

    That's why I create a trigger for her...

    However, I need to capture the SQL statement to make the update as well... However I have no way to do it, because it is always replaced by a SQL code I run to get the SESSION ID...

    Here is the trigger...
    ----------------------

    create or replace
    alert_setting_trg relaxation
    before the update on user_alert_settings
    for each line
    declare
    number of v_sid;
    v_username varchar2 (100);
    v_osuser varchar2 (100);
    v_machine varchar2 (100);
    v_program varchar2 (100);
    v_module varchar2 (100);
    v_prevsqlid varchar2 (100);
    v_sqltext varchar2 (100);

    Start
    SELECT SYS_CONTEXT ('USERENV', 'SID')
    IN v_sid
    FROM DUAL;

    SELECT the username, osuser, machine, program, module, PREV_SQL_ID
    IN v_username, v_osuser, v_machine, v_program, v_module, v_prevsqlid
    SESSION $ v where SID = v_sid;

    SELECT sql_text
    IN v_sqltext
    V $ sqltext
    WHERE SQL_ID = v_prevsqlid;

    INSERT INTO alert_settings_log
    VALUES (sysdate, v_sid, v_username, v_osuser, v_machine, v_program, v_module, v_sqltext,: new.user_id,: new.service_grp,: new.alert_setting_id,: old.alert_active,: new.alert_active);

    end;


    Now the problem is that...
    the capture of SQL statement will always be

    SELECT SYS_CONTEXT ('USERENV', 'SID')
    IN v_sid
    FROM DUAL;


    and not the UPDATE table statement.

    1st quarter) I have to get the SID of the session before I could know what SQL it is running.

    However, once I get the SID, the SQL to UPDATE the SID is run changes the SQL that gets the SID.

    What can I do?

    Rgds,
    Noob

    Instead of to the request in three steps, do it in 1. Replace all three of your queries with something like:

    SELECT s.username, s.osuser, s.machine, s.program, s.module, q.sql_text
    INTO v_username, v_osuser, v_machine, v_program, v_module, v_sqltext
    FROM v$session s, v$sql_area q
    WHERE s.sid = SYS_CONTEXT('USERENV','SID') and
          s.prev_sql_id = q.sql_id
    

    John

Maybe you are looking for