12

I want to create a table inside of a procedure. I tried to put the create query in a string then execute immediate the string. For example:

create or replace procedure hr.temp is
   var1 varchar2(4000);
begin
   var1:='create table hr.temp(
          id number)';
   execute immediate var1;
end temp;

But when I execute this procedure I get the error:

ORA-00911: invalid character
ORA-06512: at "SYS.TEMP", line 6
.
.
.

Is there any way I can do this?

8
  • 4
    Do NOT create procedures in the SYS account. Don't do it. Ever. (Once you move that procedure to a proper account: inside a dynamic SQL you must not specify the ; character. Execute immediate can execute only a single statement anyway, so no termination character is required)
    – user330315
    Jan 20, 2014 at 11:36
  • due to your suggestion i changed it to hr schema,but the result is the same Jan 20, 2014 at 11:40
  • i removed the ; character,now when i execute the procedure,it begin but never end,it's like procedure is in the loop that never end! Jan 20, 2014 at 11:46
  • 3
    Your example still includes the ;. Btw: you can't create a table with the same name as an existing procedure. But apart from that this should work fine: sqlfiddle.com/#!4/37847/2
    – user330315
    Jan 20, 2014 at 11:50
  • 2
    @jalalrasooly The name of procedure and table should be distinct, not the same. Did you change both to temp2? Jan 20, 2014 at 12:20

2 Answers 2

15

Try this. It should work...

create or replace procedure hr.temp is
   var1 varchar2(4000);
BEGIN
   var1:='create table hr.temp2(
          id number)';
   EXECUTE IMMEDIATE var1;
end temp;
0

the answer of m.r 'a_horse_with_no_name' is right. The first point is I should not create a table in sys schema.

The second point is in a dynamic sql I should not use ; character in dynamic sql.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.