0

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;
William Robertson
  • 15,273
  • 4
  • 38
  • 44
  • 2
    How do you know it *hangs*? Maybe it is counting rows in some huge tables. – Littlefoot May 13 '19 at 19:39
  • it could be, i let it run for 20 mins or so but its does not come back with anything. from what ive seem my DBs are not that big. maybe 600,000 records would be huge. I will let it run an hour ans see what happens.Perhaps im being impatient. – NewDudeCoder May 13 '19 at 20:38
  • 1
    Note that you won't see any output until the whole procedure has finished. So - no table-by-table (as soon as you get the result from one of them), but all of them at the very end. – Littlefoot May 13 '19 at 20:57
  • @NewDudeCoder I suggest you look at [this answer](https://stackoverflow.com/a/9614022/409172). Searching once-per-table instead of once-per-column will be significantly faster. Oracle's data is usually stored in rows, in blocks. When the system reads any value for a single row, the other values are put in memory almost for free, so you might as well check them all at the same time. – Jon Heller May 14 '19 at 00:54
  • oh yeh that is much better. thank you. – NewDudeCoder May 14 '19 at 15:42

0 Answers0