This is a follow on question to Vincent Malgrat's answer to this question. I can't find the correct syntax to use when you need to use the same bind variable multiple times when using OPEN...FOR
dynamic SQL. You can see the syntax for EXECUTE IMMEDIATE
here (see "Using Duplicate Placeholders with Dynamic SQL") … but not for OPEN...FOR
. Does the syntax differ with duplicate placeholders when using OPEN...FOR
? I'm using Oracle 12c. This is in a PL/SQL package not an anonymous block.
For example, this example from Oracle's own documentation works fine:
DECLARE
TYPE EmpCurTyp IS REF CURSOR;
emp_cv EmpCurTyp;
emp_rec emp%ROWTYPE;
sql_stmt VARCHAR2(200);
my_job VARCHAR2(15) := 'CLERK';
BEGIN
sql_stmt := 'SELECT * FROM emp WHERE job = :j';
OPEN emp_cv FOR sql_stmt USING my_job;
LOOP
FETCH emp_cv INTO emp_rec;
EXIT WHEN emp_cv%NOTFOUND;
-- process record
END LOOP;
CLOSE emp_cv;
END;
/
But if you need to reference the :j
bind variable more than once, how do you do it in a case like this where :j
is referenced twice?
sql_stmt := 'SELECT * FROM emp WHERE (job = :j AND name = :n) OR (job = :j AND age = :a)' ;
I have tried
OPEN emp_cv FOR sql_stmt USING my_job, my_name, my_age;
and
OPEN emp_cv FOR sql_stmt USING my_job, my_name, my_age, my_job;
and in both cases it gives this error:
ORA-01008: not all variables bound