如果我执行以下查询,那么我将收到无效字符。
declare
sql_query varchar2(4000):='delete from ';
begin
for i in (
select table_name,column_name from User_tab_cols where column_name like '%PROFILE_ID' and table_name not like 'GSC%' and table_name not like '%BKP%' order by 1)
loop
sql_query :=sql_query ||i.table_name;
sql_query:=sql_query || ' where '||i.column_name ||' = ' ||32052721 ||';';
execute immediate sql_query;
commit;
end loop;
end;
请在这个问题上帮助我。
非常感谢你的帮助。
我使用的是Oracle 11.2.0.4.0版本。
答案 0 :(得分:1)
我想,你需要为循环的每次迭代启动sql_query。例如,像这样:
declare
sql_query varchar2(4000);
begin
for i in (
select table_name,column_name from User_tab_cols where column_name like '%PROFILE_ID' and table_name not like 'GSC%' and table_name not like '%BKP%' order by 1)
loop
sql_query :='delete from ';
sql_query :=sql_query ||i.table_name;
sql_query:=sql_query || ' where '||i.column_name ||' = 32052721';
execute immediate sql_query;
commit;
end loop;
end;
答案 1 :(得分:1)
试试这个:还有一个' ;
'在你的动态字符串中。
下面:
sql_query:= sql_query || '其中' || i.column_name ||' =' || 32052721 ||&#39 ;;&#39 ;;
DECLARE
sql_query VARCHAR2 (4000) := 'delete from ';
BEGIN
FOR i
IN ( SELECT table_name, column_name
FROM User_tab_cols
WHERE column_name LIKE '%PROFILE_ID'
AND table_name NOT LIKE 'GSC%'
AND table_name NOT LIKE '%BKP%'
ORDER BY 1)
LOOP
sql_query := sql_query || i.table_name;
sql_query :=
sql_query || ' where ' || i.column_name || ' = ' || 32052721 ;
EXECUTE IMMEDIATE sql_query;
Sql_query:=' ';
COMMIT;
END LOOP;
END;
答案 2 :(得分:0)
您需要在每个循环上重新初始化查询,并省略尾随分号。
DECLARE
l_sql_query VARCHAR2 (2000);
BEGIN
FOR i IN ( SELECT table_name, column_name
FROM user_tab_cols
WHERE column_name LIKE '%PROFILE_ID'
AND table_name NOT LIKE 'GSC%'
AND table_name NOT LIKE '%BKP%'
ORDER BY 1)
LOOP
l_sql_query := ' delete from ' || i.table_name || ' where ' || i.column_name || ' = ' || 32052721;
EXECUTE IMMEDIATE l_sql_query;
COMMIT;
END LOOP;
END;