0

I am trying to create a procedure that accepts two table names as parameters, and then copies the rows from one table into another, one at a time. I know bulk insert and SELECT INTO are better ways to do this, but bulk insert isn't working because the table triggers throw mutation errors when I insert more than one row at once.

I've seen other answers that recommend using dynamic SQL, but I'm stuck on how to define the cursor that way.

CREATE OR REPLACE PROCEDURE TABLE_INSERT(
    donor_t IN VARCHAR2,
    empty_t IN VARCHAR2
    )
AS
    CURSOR C1 IS 
        SELECT * FROM donor_t;
BEGIN
    FOR row IN C1
    LOOP
        INSERT INTO empty_t VALUES row;
    END LOOP;
END;

When compiled as written above, compiler throws ORA-00942: table or view does not exist. When compiled with table names hard-coded in, this function inserts the rows as expected, without errors.

3
  • Basically what you are trying to do is an insert as select, why not do that instead? Processing data row by row is very bad for performance. Commented Jun 22, 2017 at 13:14
  • One option is to look at your triggers and consider if they can be disabled for the bulk operation and re-enabled afterwards. Obviously your bulk operation will need to deal with the business logic performed by the triggers. Otherwise try the Oracle docs for examples - docs.oracle.com/cd/B28359_01/appdev.111/b28370/… Commented Jun 22, 2017 at 13:16
  • 1
    @BriteSponge Didn't even occur to me to see if I could disable the triggers and re-enable them after insert; that solution worked like a charm. Commented Jun 22, 2017 at 14:03

1 Answer 1

1

try this instead:

CREATE OR REPLACE PROCEDURE TABLE_INSERT(
donor_t IN VARCHAR2,
empty_t IN VARCHAR2
)
AS 
  V_SQL VARCHAR2(1000);
BEGIN
    V_SQL := 'INSERT INTO ' || empty_t || ' SELECT * FROM ' ||  donor_t;
    EXECUTE IMMEDIATE V_SQL;
END;
/
Sign up to request clarification or add additional context in comments.

1 Comment

That's a good solution for a dynamic insert statement, but it's unfortunately still causing the trigger to throw mutation errors since it's not one-at-a-time inserts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.