Search All Fields In All Tables For A Specific Value (Oracle)

前端 未结 16 2283
一整个雨季
一整个雨季 2020-11-22 01:05

Is it possible to search every field of every table for a particular value in Oracle?

There are hundreds of tables with thousands of rows in some tables so I know th

16条回答
  •  滥情空心
    2020-11-22 01:39

    if we know the table and colum names but want to find out the number of times string is appearing for each schema:

    Declare
    
    owner VARCHAR2(1000);
    tbl VARCHAR2(1000);
    cnt number;
    ct number;
    str_sql varchar2(1000);
    reason varchar2(1000);
    x varchar2(1000):='%string_to_be_searched%';
    
    cursor csr is select owner,table_name 
    from all_tables where table_name ='table_name';
    
    type rec1 is record (
    ct VARCHAR2(1000));
    
    type rec is record (
    owner VARCHAR2(1000):='',
    table_name VARCHAR2(1000):='');
    
    rec2 rec;
    rec3 rec1;
    begin
    
    for rec2 in csr loop
    
    --str_sql:= 'select count(*) from '||rec.owner||'.'||rec.table_name||' where CTV_REMARKS like '||chr(39)||x||chr(39);
    --dbms_output.put_line(str_sql);
    --execute immediate str_sql
    
    execute immediate 'select count(*) from '||rec2.owner||'.'||rec2.table_name||' where column_name like '||chr(39)||x||chr(39)
    into rec3;
    if rec3.ct <> 0 then
    dbms_output.put_line(rec2.owner||','||rec3.ct);
    else null;
    end if;
    end loop;
    end;
    

提交回复
热议问题