Trigger to copy records from one table to another;  ORA-04091:

Hello,
I'm trying to create a trigger that will move data from one table to another.
I have two tables (Trial1, Trial2) Both of them contains the same attributes (code, c_index)
I want to move each new record inserted in (code in Trial1) to (code in Trial2)
This is my trigger:
Create or replace trigger trg_move_to_trial2
After insert on Trial1
for each row
begin
insert into Trial2 (code)
select :new.code from Trial1;
end;It compiled, but when I insert new (code) record into (Trial1) it display this error:
Error report:
SQL Error: ORA-04091: table STU101.TRIAL1 is mutating, trigger/function may not see it
ORA-06512: at "STU101.TRG_MOVE_TO_TRIAL2", line 3
ORA-04088: error during execution of trigger 'STU101.TRG_MOVE_TO_TRIAL2'
04091. 00000 - "table %s.%s 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.I know what does this error mean, but I don't how to fix the error.
I tried to change the (After insert on Trial1) to be (Before insert on Trial1); that worked, but not in the right way. When I insert new value into (code in Trial1) and refreshed Trial2 table, as much as records I have in Trial 2 they will be duplicated. E.g.
Trial2
code
111
222
333when I insert in Trial1
Trial1
code
444
Trial2 will be:
code
111
222
333
444
444
444Can you please tell me how to solve this issue?
Regards,
Edited by: 1002059 on Apr 23, 2013 5:36 PM

You should not select from Trial1 - you have the data already. Just insert that value.
Create or replace trigger trg_move_to_trial2
After insert on Trial1
for each row
begin
insert into Trial2 (code)
values (:new.code);
end; regards,
David

Similar Messages

  • Create Trigger to insert records from one table to another

    I created the below trigger to move data from one table to another after records have been inserted from another table to that table. What I need done is that each time records have been inserted into TEST_TBL, one of these actions codes should be implimented: U-update, N-insert, D-delete and inserted into TEST_TBL1. But each time I run the script, I get bunch of errors. Please see the script below: - Your help will be appreciated.
    create or replace
    trigger POWER_tr
    after update or insert or delete ON test_tbl
    for each row
    begin
    if updating then
    insert into test_tbl1
    (idx, mke, ctl, ori, nam, sex, rac, pob, dob, hgt, action_code, date_added
    VALUES
    (idx, mke, ctl, ori, nam, sex, rac, pob, dob, hgt, 'U', sysdate);
    ELSif INSERTING then
    insert insert into test_tbl1
    (idx, mke, ctl, ori, nam, sex, rac, pob, dob, hgt, action_code, date_added
    VALUES
    (idx, mke, ctl, ori, nam, sex, rac, pob, dob, hgt, 'N', sysdate);
    ELSIF deleting then
    insert into test_tbl1
    (idx, mke, ctl, ori, nam, sex, rac, pob, dob, hgt, action_code, date_added
    VALUES
    (idx, mke, ctl, ori, nam, sex, rac, pob, dob, hgt, 'D', sysdate);
    END IF;
    END POWER_tr;
    Thank you,
    Albert Zaza
    Edited by: azaza on Mar 1, 2009 4:14 PM

    Hello
    At the end of trigger show errors / and post what errors are you getting exactly?
    Here is a simple example for your reference, this will save old values in history table; you can replace it with new values if that's what do you want.
    CREATE OR REPLACE TRIGGER TRG_DU
       AFTER DELETE OR UPDATE
       ON EMPLOYEE    REFERENCING NEW AS New OLD AS Old
       FOR EACH ROW
    DECLARE
    BEGIN
       IF UPDATING
       THEN
          INSERT INTO employee_hist
            VALUES   ('UPDATE',
                      :OLD.empid,
                      :OLD.name,
                      :OLD.deptid);
       ELSIF DELETING
       THEN
          INSERT INTO employee_hist
            VALUES   ('UPDATE',
                      :OLD.empid,
                      :OLD.name,
                      :OLD.deptid);
       END IF;
    EXCEPTION
       WHEN OTHERS
       THEN
          -- Consider logging the error and then re-raise
          RAISE;
    END TRG_DU;
    /Regards
    Edited by: OrionNet on Mar 1, 2009 7:27 PM

  • T code to Copy Data from one table to another table

    Hi,
    I want copy all data of one table to its copy table. Anyone knows transaction code to copy data from one table to another table.
    Regards,
    Jigar Thakkar.

    Hi
    Create a small program.
    Extract data from T1 - database table and put it in one internal table - itab1.
    loop the itab1 data .............
        insert itab1 into tab2.   (tab2 - second database table)
    endloop.
    try this....
    hope it works....

  • How to copy data from one table to another (in other database)

    Hi. I would like to copy all rows from one table to another (and not use BC4J). Tables can be in various databases. I have already 2 connections and I am able to browse source table using
    ResultSet rset = stmt.executeQuery("select ...");
    But I would not like to create special insert statement for every row . There will be problems with date formats etc and it will be slow. Can I use retrieved ResultSet somehow ? Maybe with method insertRow, but how, if ResultSet is based on select statement and want to insert into target table? Please point me in the right direction. Thanks.

    No tools please, it must be common solution. We suceeded in converting our BC4J aplication to PostgreSQL, the MSSQL will be next. So we want to write simple aplication, which could transfer data from our tables between these 3 servers.

  • How to move records from one table to another?

    Is there a way to copy data and column names from one table to another table?
    I am trying to copy one production database table onto the development database. we are using Oracle 10g. I tried exporting to an xls file and importing to the other table but this needs to create destination table with the column names. Is there any better approach to achieve this?

    Hello,
    You can create dblink between production and dev and from dev just
    *1. create table mytable as select * from mytable@dblink;**2. Use of export/import or datapump*
    Example
       On prod
       exp username/password tables=mytable1,mytable2 file=mytableexport.dmp log=mytableexport.log
       On dev
       imp username/password tables=mytable1,mytable2 file=mytableexport.dmp log=mytableimport.log*3. Datapump*
    http://www.oracle-base.com/articles/10g/OracleDataPump10g.php
    *4. Generate csv formatted file and use sqlldr or external*
    External Table: http://www.adp-gmbh.ch/ora/misc/ext_table.html
    Sqlldr: http://www.psoug.org/reference/sqlloader.html
    Regards
    Edited by: OrionNet on Feb 13, 2009 6:52 PM

  • Copy data from one Table to another Table

    How can I copy data from one Oracle Table to another Oracle Table on a different server? Question 2: How can I clear all of the data in one Table with a single SQL script?
    Thanks...

    Question 1:
    I assume you have the privileges. If you don't, ask the DBA to give them to you. Then
    1. Login to database_source (It could be either the source or the target. Let's assume it's the source.)
    2. Create a database link to database_target: CREATE DATABASE LINK link_to_database_target CONNECT TO myuserid IDENTIFIED BY mypassword USING 'database_target'; Note the single quotes.
    3. Copy the table data: INSERT INTO targetowner.mytable@link_to_database_target SELECT * FROM sourceowner.mytable; COMMIT;
    Question 2:
    You have two options, but you may not have privileges for both.
    Option 1:
    DELETE FROM tableowner.tablename; COMMIT;
    Advantage: Since this is a DML (Data Manipulation Language) statement, you have to commit the transaction. Also, the data will be gone but the table size is NOT changed, so it's ready for accepting replacement data. DML statements can simply be executed not only from SQL scripts, but from PL/SQL scripts as well.
    Disadvantage: Slow, because all record deletion is logged, so you can recover from it by issuing a ROLLBACK; instead of the COMMIT; above. The table size is NOT changed, so if you are short of disk space or tablespace space, you have not resolved the issue.
    Option 2:
    TRUNCATE TABLE tableowner.tablename;
    Advantage: Since this is a DDL (Data Definition Language) command, you do NOT have to commit the transaction. (DDL commands automatically commit both before and after their execution.) The table size will be changed back to the initial extent size which is the minimum size a table can have and can only be set when the table is created. If it needs to be changed, the table has to be dropped and recreated with a different initial extent size. The statement execution of this command is not logged, therefore it's much faster then the DELETE.
    Disadvantage: No rollback. Being a DDL, this command cannot be executed straight from PL/SQL. If you need to issue this within PL/SQL, you will have to use dynamic SQL.

  • Copying data from one table to another table thru java

    Hi
    I have to copy data from table emp in Database A to table emp in Database B. My input would be table name and number of rows to be fetched. these rows i need to insert in table B.
    The problem over here is I won't be having any info. of table emp i.e the number of columns it has and their type.
    So is their any way i can copy the data from one table to other without having the info abt the number of cols and their type.
    TIA

    Cross post - http://forum.java.sun.com/thread.jspa?threadID=5169293&messageID=9649839#9649839

  • How to pass selected records from one table to another ?

    Hi,
    In my view i have designed a table with certain records.  I need to pass only specific records of this table to another table which been designed in another view. Can anybody please give sum idea for this.
    Rgds
    Sudhanshu

    hi,
    Refer the below  code:
    1. I have selected some data from the table.
    2. The selected data is moved to some other internal table.
    3. Internal table is further binded to the node in which data is to be shown.
    data : lo_nd type ref to if_wd_context_node,
      lo_nd1 type ref to if_wd_context_node,
      lt_temp type wdr_context_element_set,
      wa_temp type ref to if_wd_context_element,
      ls_node1 type sflight,
      lt_node1 type STANDARD TABLE OF sflight.
    lo_nd = wd_context->get_child_node('CN_MAIN').  <CN_MAIN is my node>
      CALL METHOD lo_nd->get_selected_elements  <here selected data is moved to lt_temp>
       RECEIVING
           set = lt_temp.
      loop at lt_temp INTO wa_temp.
          CALL METHOD wa_temp->get_static_attributes
          IMPORTING
            static_attributes = ls_node1.       <Selected data in work area.>
        APPEND ls_node1 TO lt_node1.  < Data moved to internal Table>
        CLEAR ls_node1.
      ENDLOOP.
    Finally Internal table is binded to the node required.
      lo_nd1 = wd_context->get_child_node('CN_MAIN2').
      lo_nd1->bind_table( lt_node1 ).
    In your case , map this CN_MAIN2 with the Component Controller and from component controller you can access data in your second view also.
    I hope it helps.
    Thanx.
    Saurav.

  • Copy records of one table into another using Update statement

    Supposing I have 2 tables test and test1.
    Test has columns col1 and col2. and test1 has only one table col1.
    select * from test returns:
    COL1 COL2
    a
    b
    c
    d
    e
    select * from test1 returns
    COL1
    p
    q
    r
    s
    t
    Suppose i want to copy values of test1.col1 to test.col2 so tht final result of
    select * from test should be
    COL1 COL2
    a p
    b q
    c r
    d s
    e t
    I found a query in the OCP introduction book:
    update test set col2=(select col1 from test11)
    this works fine only when we have a single record in test1 table but fails otherwise.
    how can this be achieved using the update statement ?

    SQL> desc test
    Name Null? Type
    COL1 VARCHAR2(10)
    COL2 VARCHAR2(30)
    SQL> desc test1
    Name Null? Type
    COL1 VARCHAR2(10)
    SQL> insert into test values ('a','');
    1 row created.
    SQL> insert into test values ('b','');
    1 row created.
    SQL> insert into test values ('c','');
    1 row created.
    SQL> insert into test values ('d','');
    1 row created.
    SQL> insert into test1 values ('e');
    1 row created.
    SQL> insert into test1 values ('f');
    1 row created.
    SQL> insert into test1 values ('g');
    1 row created.
    SQL> insert into test1 values ('h');
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> ed
    Wrote file afiedt.buf
    1 select a.col1, b.col1, a.rn from
    2 (select row_number() over (order by col1) rn, col1 from test) a,
    3 (select row_number() over (order by col1) rn, col1 from test1) b
    4* where a.rn = b.rn
    SQL> /
    COL1 COL1 RN
    a e 1
    b f 2
    c g 3
    d h 4
    SQL> ed
    Wrote file afiedt.buf
    1 update test set col1 =
    2 (
    3 select forupd from (
    4 select a.col1, b.col1 test1col, a.col1||' '||b.col1 forupd, a.rn from
    5 (select row_number() over (order by col1) rn, col1 from test) a,
    6 (select row_number() over (order by col1) rn, col1 from test1) b
    7 where a.rn = b.rn
    8* ) z where z.col1 = test.col1)
    SQL> /
    4 rows updated.
    SQL> commit;
    Commit complete.
    SQL> select * from test;
    COL1 COL2
    a e
    b f
    c g
    d h
    SQL>
    This will work only if you have the same number of lines in COL1 in both tables. If you have different number of lines, then you need to code more at join (outer, inner).
    But for complicated cases, please post sample data.
    Michael
    PS: And supossing that the values are distinct in TEST table. Else update will fail, because more rows will be retrived.
    If more values in TEST table in COL1, then you need to assign a row number on TEST also, and in WHERE of query you need to place one more join condition:
    something like:
    8* ) z where z.col1 = test.col1
    and z.rn = test.rn)
    SQL> /
    But as i said, get more specifications.
    Message was edited by:
    kjt
    Added the PS.

  • How to copy file from one table to another table at another database

    I need to transfer my tables from one workspace and schema to another workspace and schema. Basically I need to create again all the tables at this new schema. How could I transfer data from tha table at old schema to the table at new schema when this table has files stored in it? (data type is blob)
    thank you so much,
    Silver

    Hello Silver,
    Depending which database you're using (if it's available) I would recommend to use datapump.
    Datapump allows you to copy an entire schema to another database, it's the "new" export/import you might now.
    Regards,
    Dimitri
    http://dgielis.blogspot.com/
    http://www.apex-evangelists.com/
    http://www.apexblogs.info/
    REWARDS: Please remember to mark helpful or correct posts on the forum

  • Copy data from one table to another

    Hello everyone,
    I have a student table with fields sno, sname. I created another table student1 with same fields sno, sname and with one new field class.
    Now i want to copy data from sno, sname of student table to sno, sname of student1 table when the class field in styudent1 has no data i.e., its null.
    Could any one let me know how to do this?
    Thanks,
    Prathima

    i want to copy data from sno, sname of student table to sno, sname
    of student1 table when the class field in styudent1 has no dataSo what is the join condition? What column in STUDENT1 tells us what row in STUDENT to copy?
    Cheers, APC
    Blog : http://radiofreetooting.blogspot.com/

  • Error when copying items from one table to another

    I am trying to copy data from 2 archive tables within an
    Access database to their twin production tables. The first table
    copies perfectly with the code:
    <CFQUERY NAME="moveArchAttach" datasource="faaco2">
    INSERT INTO tbl_Attachment ( PostID, Attachment, AttachNote,
    AttachName )
    SELECT tbl_Attachment_Archived.PostID,
    tbl_Attachment_Archived.Attachment,
    tbl_Attachment_Archived.AttachNote,
    tbl_Attachment_Archived.AttachName
    FROM tbl_Attachment_Archived
    </CFQUERY>
    However, when trying to use similar code on the other table:
    <CFQUERY NAME="moveArchPost" datasource="faaco2">
    INSERT INTO tbl_Posting ( Title, FirstName, LastName,
    PhoneNumber, RoutingSymbol, Email, IniDate, ComDate, SOLNBR,
    PhaseCode, ProcCode, Posting, Password, RegionCode )
    SELECT tbl_Posting_Archived.Title,
    tbl_Posting_Archived.FirstName, tbl_Posting_Archived.LastName,
    tbl_Posting_Archived.PhoneNumber,
    tbl_Posting_Archived.RoutingSymbol, tbl_Posting.Email,
    tbl_Posting.IniDate, tbl_Posting.ComDate, tbl_Posting.SOLNBR,
    tbl_Posting.PhaseCode, tbl_Posting.ProcCode, tbl_Posting.Posting,
    tbl_Posting.Password, tbl_Posting.RegionCode
    FROM tbl_Posting_Archived
    </CFQUERY>
    I receive the error:
    [Microsoft][ODBC Microsoft Access Driver] Too few parameters.
    Expected 9.
    Any help would be greatly appreciated, I am at a loss and
    I've tried everything. Thank you.

    I realized my error immediately after posting this topic and
    subsequently have resolved this error. On 9 of the values, I have
    listed the wrong table in the SELECT statement. This is why one
    needs to take breaks instead of staring at a screen forever :).
    Thank you anyway!

  • Automatic copying row from one table into another one

    Hi,
    I am looking for some help on how to do the following:
    I use Number to track my finances. I have two tables - one for my checking account and the other one for my cash account. When I withdraw cash from my checking account I record a transfer or debit transaction in my checking account table (in the type column of this table I enter "transfer" and in the category column of this table I enter "cash account"); Obviously I have to record a matching transaction in my cash account where the category column shall read "checking account". Both records represent one and the same transaction. In order not to enter this transaction twice I would like to "automate" this process so that once I enter the transaction in either of the two table (checking or cash) the matching entry automatically appears in the other table. Is there any way to do this.
    Thank you,
    Evgeny

    You can use Connection#getMetaData() to retrieve information about the tables and the columns of the table.
    After all, it is better to gain information about the table first and then issue a query in the form of "INSERT INTO table1 SELECT * FROM table2", including the eventual column selections and/or data conversions at SQL level.

  • Insert record from one table to another with help of cursor

    Plz help!!!
    tables are - 1. country( country_id pk, country_name, region_id)
    2. a( country_id , country_name, region_id)
    table a data are
    1 a 1
    2 b 2
    3 c 3
    null d 4
    5 e 5
    6 f 6
    7 g 7
    insert record from table a to country table with help of cursor, insert all not null records.
    this procedure does not give correct result
    create or replace
    procedure amit as
    cursor c1 is select * from a;
    rw a%rowtype;
    begin
    open c1;
    fetch c1 into rw;
    while(c1%found)
    loop
    insert into countries values(rw.country_id,rw.country_name,rw.region_id);
    commit;
    fetch c1 into rw;
    if rw.country_id is null then
    fetch c1 into rw;
    end if;
    end loop;
    close c1;
    exception
    when others then
    dbms_output.put_line('exception name= '||rw.country_name);
    end;

    bluefrog wrote:
    You don't need cursor at all;
    create or replace procedure amit as
    begin
    insert into countries (Country_ID, Country_Name, Region_ID)
    (select a.Country_ID
    ,a.Country_Name
    ,a.Region_ID
    from a
    dbms_output.put_line('Rows inserted : ' || sql%rowcount);
    commit;
    end;
    Bluefrog you missed where clause. :)
       insert into countries (Country_ID, Country_Name, Region_ID)
       (select a.Country_ID
              ,a.Country_Name
              ,a.Region_ID
        from a
        where country_id is not null
    );

  • Copying Records from one form to another in Forms 4.5 (**URGENT**)

    Hello Gurus,
    I have Form A through which I zoom to Form B (using open form). The user selects some records in form B. Those selected records should be displayed in form A after the user clicks a button say 'copy' in Form B.
    What is the best possible method to do this.
    Thanks in Advance.
    Regards
    Mallik

    You can accomplish this in two ways. To my knowledge you are not dealing with database tables in this issue. I guess. If you are then committing to the table and then requerying is the best thing to do.
    You can use either PL/SQL table types or Dynamic Record Groups to accomplish this task. PL/SQL tables are only supported from PL/SQL Ver 2.0 in forms. As long as you are using FORMS 5.0 or above then you can any of the above methods. My example here will tell you how to use PL/SQL table/record types...
    0. Create a PL/SQL Record type in a common library that you attach to form A and form B. Lets name it as FORMB_RECS.
    1. In Form B write WHEN-CHECKBOX-CHANGED trigger for the check box that you are using to select records. In that please copy the record primary key or all values to the FORMB_RECS declared in the common library.
    2. In the COPY button of Form B, copy the number of records in record group to global variable :GLOBAL.Copy_RecCnt and navigate to form A.
    3. In WHEN-FORM-NAVIGATE trigger of form A, check and see if :GLOBAL.Copy_RecCnt is > 0. If so then loop through the FORMB_RECS in common library and populte the desired block in form A.
    HTH,
    -- Raam.

Maybe you are looking for

  • Web gallery not working

    Can't seem to get my web galleries to work. i end up with a white screen. The look fine in preview and in dreamweaver but not when i actually load them to my site. I have an older one web gallery that still works. here is a sample problem page http:/

  • Nokia N78 firmware update

    Whenever i choose to update my phone (N78) software, after donwload completes from internet, at around 13~15% of updgradtion process communication (USB link) with mobile phone drops and systems propts a new hardware is found (Nokia ROM Driver) but af

  • Credit Memos and KE27 periodic valuation

    Hi We created a credit memo without reference to a billing document with actual goods return in period 03 2012 (June 2012). Then we ran ML settlement. We when we ran KE27  we got the error message MLCCS030 no valuation exists for material # We did no

  • Booting from disk image of iMac, off external, w/ MacBook..am I the 1st ?

    Hi there, I'm currently between iMacs. Apple is graciously replacing my troubled early `06 / 20" Intel iMac ( which I FedEx'd out on 3/1/07 ) w/ a current model ( which is in the brink of shipping to me ). This one I've upgraded to 24 " / 750 GB on t

  • Error when launching flex App?

    ReferenceError: Error #1056: Cannot create property reserved on _index_mx_managers_SystemManager. What does this error mean? I have been debugging it for about a week now :)