Oracle 11g“绑定变量不存在" [英] Oracle 11g "Bind variable does not exist"
问题描述
在以下代码中,我在第15行收到"ORA01006绑定变量不存在"错误:
I am getting an "ORA01006 Bind variable does not exist at line 15 "error in the following code:
DECLARE
v_search_string varchar2(4000) := 'OK';
v_query_str VARCHAR2(4000);
match_count integer;
BEGIN
FOR t IN (SELECT owner,
table_name,
column_name
FROM all_tab_columns
WHERE data_type in ('CHAR', 'VARCHAR2', 'NCHAR', 'NVARCHAR2') And TABLE_NAME = 'T1' And OWNER = 'O1')
LOOP
Begin
v_query_str := 'SELECT COUNT(*) FROM '|| t.table_name || ' WHERE ' || t.column_name || ' Like ''' || '%:1%' || '''';
dbms_output.put_line(v_query_str);
EXECUTE Immediate v_query_str
INTO match_count
USING v_search_string;
IF match_count >= 0 THEN
dbms_output.put_line( t.owner || '.' || t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END;
END LOOP;
END;
我只是试图遍历表中的所有字符列,并计算每个字符列中与v_search_string值匹配的值.
I'm just trying to loop through all the character columns in the table and count how many values in each match the v_search_string value.
"dbms_output.put_line(v_query_str);"行打印一行: 从T1 WHERE Col1中选择COUNT(*),例如'%:1%'
The line "dbms_output.put_line(v_query_str);" prints one line: SELECT COUNT(*) FROM T1 WHERE Col1 Like '%:1%'
表中有10列是指定类型.
There are 10 columns in the table that are the specified types.
那里显然有一个绑定变量(%1),所以我不知道发生了什么.
There is obviously a bind variable there (%1), so I can't figure out what's going on.
推荐答案
按如下所示构成字符串.
Form the string like this below.
t.column_name || ' Like ''%''||:1||''%'''
绑定变量不应包含在单引号中,因为它将被视为String文字.因此,当您使用USING
时,它就此排他性地结束了.
Bind variable should not be included within single quotes, as it would be treated as a String literal instead. So when you used USING
it ended up with this excpetion.
这篇关于Oracle 11g“绑定变量不存在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!