Search This Blog

Tuesday, March 12, 2013

PL/SQL How to find any data in a schema

in this deli-ma 
I needed to find a value for a column in my oracle database but i don't know which table or column it's stored in
How can I search for a specific or like %% data as I do in
select * from SYS.dba_source
is there a table like that
Column Name ID  Data Type             Null?         Comments

OWNER   1   VARCHAR2 (30 Byte)       Y  
NAME    2   VARCHAR2 (30 Byte)       Y    Name of the object
TYPE    3   VARCHAR2 (12 Byte)       Y    

Type of the object:
"TYPE", "TYPE BODY", "PROCEDURE", "FUNCTION",
"PACKAGE", "PACKAGE BODY" or "JAVA SOURCE"

LINE    4   NUMBER                  Y    Line number of this line of source
TEXT    5   VARCHAR2 (4000 Byte)    Y    Source text



Then Answer

Imagine, there are a few tables in your schema and you want to find a specific value in all columns within these tables. Ideally, there would be an sql function like
select * from * where any(column) = 'value';
Unfortunately, there is no such function. However, a PL/SQL function can be written that does that. The following function iterates over all character columns in all tables of the current schema and tries to find val in them.
  create or replace function find_in_schema(val varchar2) 
    return varchar2 is
      v_old_table user_tab_columns.table_name%type;
      v_where     Varchar2(4000);
      v_first_col boolean := true;
      type rc     is ref cursor;
      c           rc;
      v_rowid     varchar2(20);

    begin
      for r in (
        select
          t.*
        from
          user_tab_cols t, user_all_tables a
        where t.table_name = a.table_name
          and t.data_type like '%CHAR%'
        order by t.table_name) loop

        if v_old_table is null then
          v_old_table := r.table_name;
        end if;

        if v_old_table <> r.table_name then
          v_first_col := true;

          -- dbms_output.put_line('searching ' || v_old_table);

          open c for 'select rowid from "' || v_old_table || '" ' || v_where;

          fetch c into v_rowid;
          loop
            exit when c%notfound;
            dbms_output.put_line('  rowid: ' || v_rowid || ' in ' || v_old_table);
            fetch c into v_rowid;
          end loop;

          v_old_table := r.table_name;
        end if;

        if v_first_col then
          v_where := ' where ' || r.column_name || ' like ''%' || val || '%''';
          v_first_col := false;
        else
          v_where := v_where || ' or ' || r.column_name || ' like ''%' || val || '%''';
        end if;

      end loop;
      return 'Success';
    end;

No comments:

Post a Comment