簡體   English   中英

select 列具有特定值的所有表中的表名

[英]select table name from all tables where column has specific value

我需要 select 列具有特定值的所有表中的表名,例如:

select table_name from all_tab_columns where column_name = 'VALUE';

但有價值的條件謝謝

您想要的是搜索數據庫中的所有列,其中一些值說“ABC”和此類表的列表。

為此,您必須編寫一個動態腳本來在一個表中選擇一個表並搜索該表中的所有列並循環遍歷它。

好吧,不清楚您要的是什么,是表的列名還是列本身的實際值。

我想這是第二個,因為你已經知道第一個。 Oracle 不在任何字典視圖中存儲列值,因為它沒有意義。 值存儲在相應的表中。

因此,一種方法可能是使用 PL/SQL,但如果您的數據庫很大,則需要很長時間。 在我的示例中,我正在尋找一個字符串,我可以通過僅應用於數據類型為 varchar2 或 char 的列來限制該字符串,並且當我正在尋找一個特定值時,我丟棄所有長度低於該值的列我在尋找。

set serveroutput on size unlimited echo on verify off timing on 
declare
vowner varchar2(128);
vtable varchar2(128);
vcolum varchar2(128);
vvalue varchar2(40) := 'TEST_TABLE';
vcount pls_integer;
begin
for c in ( select owner , table_name from all_tables order by 1 , 2 )
loop 
    vowner := c.owner;
    vtable := c.table_name ;
    for h in ( select column_name from all_tab_columns where owner = vowner and table_name = vtable 
               and data_type in ( 'CHAR' ,  'VARCHAR2' ) and data_length >= length(vvalue) 
               order by column_id )
    loop
        vcolum := h.column_name;
        execute immediate ' select count(*) from '||vowner||'.'||vtable||' where upper('||vcolum||') = '''||vvalue||''' ' into vcount ;
        if vcount > 0
        then 
            dbms_output.put_line('Found '||vvalue||' in table --> '||vowner||'.'||vtable||' | column --> '||vcolum||' ');
        end if;
    end loop;
end loop;
end;
/

我限制要查找的表的示例

SQL> set serveroutput on size unlimited echo on verify off timing on lines 200
declare
vowner varchar2(128);
vtable varchar2(128);
vcolum varchar2(128);
vvalue varchar2(40) := 'TEST_TABLE';
vcount pls_integer;
begin
for c in ( select owner , table_name from all_tables where owner = 'TEST_PERF' order by 1 , 2 )
loop
        vowner := c.owner;
        vtable := c.table_name ;
        for h in ( select column_name from all_tab_columns where owner = vowner and table_name = vtable and data_type in ( 'CHAR' ,  'VARCHAR2' ) and data_length >= length(vvalue)  order by column_id )
        loop
                vcolum := h.column_name;
                execute immediate ' select count(*) from '||vowner||'.'||vtable||' where upper('||vcolum||') = '''||vvalue||''' ' into vcount ;
                if vcount > 0
                then
                      dbms_output.put_line('Found '||vvalue||' in table --> '||vowner||'.'||vtable||' | column --> '||vcolum||' ');
                end if;
      end loop;
end loop;
end;
/

Found TEST_TABLE in table --> TEST_PERF.T | column --> C1
Found TEST_TABLE in table --> TEST_PERF.TEST_OBJECTS | column --> OBJECT_NAME

PL/SQL procedure successfully completed.

Elapsed: 00:00:17.29
SQL>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM