-1

I am running a stored proc :

CREATE OR REPLACE MYSP (runDate Date)
BEGIN 
EXECUTE IMMEDIATE 'CREATE TABLE MYTABLE AS (SELECT * FROM DATATABLE WHERE DATADATE = :1' USING runDate); 

END MYSP;

I get the error -

ORA-01027: bind variables not allowed for data definitions operations 
ORA-06512: "Database.MYSP", line 4

Can someone help where am I going wrong.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Monica
  • 169
  • 2
  • 13

1 Answers1

10

The error is pretty self-explanatory. You cannot use bind variables in a DDL statement.

It is incredibly, incredibly unlikely that you really want to create a table in Oracle dynamically. I would strongly suggest taking a step back and finding a different way to design the system so that you're not trying to create tables at runtime.

If you are absolutely convinced that you have an extraordinary need that requires creating a table at runtime, you can't use bind variables and you don't want to have mismatched parenthesis (you have an open parenthesis but no close parenthesis). Assuming that you are ignoring the time component, something like

EXECUTE IMMEDIATE 'CREATE TABLE MYTABLE ' ||
                  ' AS ' ||
                  ' SELECT * FROM DATATABLE WHERE DATADATE = to_date( ' || 
                  to_char( runDate, 'YYYY-MM-DD' ) ||
                  ', ''YYYY-MM-DD'')';

should work.

Justin Cave
  • 227,342
  • 24
  • 367
  • 384