问题
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
回答1:
You need to include the parameter twice in the USING clause:
OPEN emp_cv FOR sql_stmt USING my_job, my_job;
Here's your example, but simplified:
DECLARE
TYPE EmpCurTyp IS REF CURSOR;
emp_cv EmpCurTyp;
emp_rec varchar2(10);
sql_stmt VARCHAR2(200);
my_job VARCHAR2(15) := 'X';
BEGIN
OPEN emp_cv FOR 'select * from dual where dummy = :j or dummy = :j'
USING my_job, my_job;
LOOP
FETCH emp_cv INTO emp_rec;
EXIT WHEN emp_cv%NOTFOUND;
END LOOP;
CLOSE emp_cv;
END;
回答2:
Repeated placeholders topic is well described here https://docs.oracle.com/database/121/LNPLS/dynamic.htm#LNPLS631 . In a word, in USING clause parameters are treated by positions, so you can use variable several times in a query but for each occurrence a separate value must be provided in USING statement.
来源:https://stackoverflow.com/questions/60530497/repeating-the-same-bind-variable-multiple-times-when-using-the-open-for-dynami