i am trying to find all instances of a data value in an oracle data base. I have written dynamic sql to look in DBA_TAB_COLUMNS for a specific column name. Then i string together the dynamic slq but there are tables in the DBA_TAB_COLUMNS that do not return anything, the sql just locks up and hangs. it does this even if i run the same query sql as a single query or if i Execute Immediate. So i need to have some sort of time out or error handler in my dynamic sql. As the dynamic sql (simple select statement) just hangs up it does not return any values from some of the tables but does work well for others. so it must time out i suspect. Other than a couple tables that seem to hang the dynamic sql the code works. how do i trap the errors?
set serveroutput on size 300000;
DECLARE
CURSOR guru99_det IS
SELECT OWNER,
TABLE_NAME,
COLUMN_NAME
FROM DBA_TAB_COLUMNS
--*************************************************
--enter the field name you are searching here.
WHERE COLUMN_NAME LIKE '%STATUS%'
--************************************************
AND OWNER like '%KW%'
ORDER BY DBA_TAB_COLUMNS.OWNER;
s_string guru99_det%ROWTYPE;
sql_str VARCHAR2(32000);
cnt NUMBER;
found_cnt NUMBER;
lp_cnt NUMBER := 0;
BEGIN
OPEN guru99_det;
loop
FETCH guru99_det INTO s_string;
exit when guru99_det%notfound;
sql_str := null;
lp_cnt := lp_cnt + 1;
--BUILD THE SQL STRING
select
(
'SELECT ' ||
' COUNT(*) ' ||
' FROM ' ||
s_string.OWNER ||
'.' ||
s_string.TABLE_NAME ||
' WHERE ' ||
s_string.COLUMN_NAME ||
--***********************************************
--enter the data value you are searching for here
' LIKE ''%CA%'' '
--***********************************************
) into sql_str from dual ;
Dbms_output.put_line( CAST(lp_cnt AS CHAR) || ' sql_str is: '||sql_str);
BEGIN
EXECUTE IMMEDIATE sql_str INTO found_cnt ;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR');
END;
dbms_output.put_line('found_cnt: > ' || found_cnt ) ;
end loop;
CLOSE guru99_det;
END;