Tuesday, May 6, 2008

String in a Haystack

This is for the data reverse-engineers out there. What do you do if you have a distinctive string value making an appearance in a form or report that's in a big application package, and you have no idea what table or column that value might be calling home? Here's some fancy dancy Oracle SQL to do the trick. I would double-check with your DBA before running against production systems. Your mileage may vary.


declare
cur sys_refcursor;
val varchar2(4000);
begin
for i in (select 'select "' || atc.column_name || '"' ||
' from "' || atc.owner ||
'"."' || atc.table_name || '"' ||
' where "' || atc.column_name ||
'" like ''%' || :searchstring ||
'%''' as cmd
,atc.owner
,atc.table_name
,atc.column_name
from all_tab_columns atc
join all_objects ao on (atc.owner =
ao.owner and
atc.table_name =
ao.object_name)
where ao.object_type = 'TABLE'
and atc.data_type = 'VARCHAR2'
and ao.owner = :schema
order by 2
,3
,4)
loop
open cur for i.cmd;
loop
fetch cur
into val;
exit when cur%notfound;
dbms_output.put_line(val || ' <= ' ||
i.owner || '.' ||
i.table_name || '.' ||
i.column_name);
end loop;
end loop;
end;


There are two variables, the :searchString and the :schema. Bind/substitue with your query tool of choice.

0 comments: