0

I'm trying to execute below query but getting invalid identifier error

EXECUTE IMMEDIATE 'create table temp parallel (degree 4) nologging as 
select e.* from employee e where e.emp_id between r0 and r1';
/

r0 and r1 I'm passing as input variable of datatype number

3
  • I tried but it's giving error that bind variables are not allowed in data definition operations Commented Oct 12, 2022 at 6:43
  • I've tried this not working PROCEDURE temp_prc ( r0 in integer,r1 in integer ) IS TEMP_TBL varchar2(4000); begin TEMP_TBL:='create table temp parallel (degree 4) nologging as select e.* from employee e where e.emp_id between :r0 and : r1'; EXECUTE IMMEDIATE TEMP_TBL using r0,r1; end temp_prc; Commented Oct 12, 2022 at 7:42
  • 1
    Yes, you should get ora-01027 error. @Reza Davoudian 's answer(even along with a Stored Procedure) would be suitable for your case I think. Commented Oct 12, 2022 at 12:40

2 Answers 2

3

If you want to use dynamic SQL in a PL/SQL block try this:

DECLARE
  r0 number :=  100;
  r1 number := 1100;
BEGIN
  EXECUTE IMMEDIATE 'CREATE TABLE temp parallel (degree 4) nologging AS 
                     SELECT * 
                       FROM employee 
                      WHERE emp_id BETWEEN '||r0||' AND '||r1||' ';
END;
/
Sign up to request clarification or add additional context in comments.

1 Comment

I edited the answer just now. run the block again please. @Aman Kumar Singh
0

r0 and r1 are not bind variables: change to :r0 and :r1 and add USING clause to EXECUTE IMMEDIATE. (read the online documentation: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnpls/EXECUTE-IMMEDIATE-statement.html)

3 Comments

I tried but it's giving error that bind variables are not allowed in data definition operations
Indeed I forgot this point, only for select, update, delete, ... then you have to concat the values to built the string.
You mean like this right ? I've tried this not workingPROCEDURE temp_prc ( r0 in integer,r1 in integer ) IS TEMP_TBL varchar2(4000); begin TEMP_TBL:='create table temp parallel (degree 4) nologging as select e.* from employee e where e.emp_id'|| 'between '|| 'r0 ' ||'and ' ||'r1'; EXECUTE IMMEDIATE TEMP_TBL; end temp_prc;

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.