0

I have in a variable DDL and DML queries.

example:

 var1='insert into tb1 (id) values(1); insert into tb1 (id) values(2); insert into tb1 (id) values(3);'

I want to execute the var1 that contains several inserts. how to do that? execute immediate didnt worked.

Edit: I tryd this , It didnt work because there is ';' inside it.

 DECLARE COL_COUNT NUMBER;
 BEGIN
 EXECUTE IMMEDIATE ('insert into ex_employee (id) values (3); insert into ex_employee (id) values (4);');
 END;
 /
2
  • You need to frame it as a dynamic PL/SQL block. Commented Jun 23, 2014 at 14:00
  • I made a slight change to your version. Kindly check the same. Commented Jun 23, 2014 at 14:11

1 Answer 1

2

Embed your Inserts into a BEGIN-END anonymous block like one below.

Oracle document

DECLARE COL_COUNT NUMBER;
 BEGIN
 EXECUTE IMMEDIATE 'BEGIN insert into ex_employee (id) values (3); insert into ex_employee (id) values (4); END;';
 EXCEPTION
 WHEN OTHERS THEN
        --Handle Exception here.
 END;
 /
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, same like a Normal PL/SQL. Exception inside the dynamic execution, would be thrown to the Executor block itself.
ah tahts great , another question IF it was several blocks example: execute immediate 'begin ... end; begin ... end;' would it work ?
No,instead you can again nest the begin-end; like begin-begin-end;begin-end;end; So, it looks like only BEGIN-END

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.