当我创建以下过程时
@H_502_16@
Oracle:
Repeated Placeholder Names in Dynamic SQL Statements中存在“特殊”行为
create or replace procedure check_exec_imm( tab IN VARCHAR2,col IN VARCHAR2,col_name IN VARCHAR2 ) IS cv SYS_REFCURSOR; col_value VARCHAR2(32767); lv_query VARCHAR2(32767); BEGIN lv_query := 'SELECT ' ||col|| ' FROM ' ||tab|| ' WHERE (:1 = ''EUR'' OR :1 = ''USD'') and rownum <=1'; EXECUTE IMMEDIATE lv_query INTO col_value USING col_name ; DBMS_OUTPUT.PUT_LINE('COLUMN VALUE : ' || col_value); END;
执行该过程时,我收到以下错误
ORA-01008: not all variables bound ORA-06512: at "GRM_IV.CHECK_EXEC_IMM",line 18 ORA-06512: at line 2
当我再次给出绑定参数col_name时,该过程运行正常.
EXECUTE IMMEDIATE lv_query INTO col_value USING col_name,col_name ;
为什么oracle在此过程中表现不同.既然,它是相同的绑定变量,一个绑定参数应该足够了.. !!?请解释我的逻辑错误.
在匿名块或CALL语句中,如果名称相同,则不需要重复绑定值.
例如,这个匿名块正在工作:
DECLARE a NUMBER := 4; b NUMBER := 7; plsql_block VARCHAR2(100); BEGIN plsql_block := 'BEGIN calc_stats(:x,:x,:y,:x); END;'; EXECUTE IMMEDIATE plsql_block USING a,b; -- calc_stats(a,a,b,a) END; /
但是这个EXECUTE IMMEDIATE plsql_block使用a,b;在程序中不起作用.