简体   繁体   English

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

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

i need to select table names from all tables where column has specific value something like:我需要 select 列具有特定值的所有表中的表名,例如:

select table_name from all_tab_columns where column_name = 'VALUE';

but with value condition thanks但有价值的条件谢谢

What you want is to search all columns in the database with some values say "ABC" and list of such tables.您想要的是搜索数据库中的所有列,其中一些值说“ABC”和此类表的列表。

For this you have to write a dynamic script to pick one table at a table and search all columns in that table and loop through it.为此,您必须编写一个动态脚本来在一个表中选择一个表并搜索该表中的所有列并循环遍历它。

Well, it is not clear what are you asking for, whether is the column name of a table or the actual value of the column itself.好吧,不清楚您要的是什么,是表的列名还是列本身的实际值。

I guess it is the second, as the first one you already know.我想这是第二个,因为你已经知道第一个。 Oracle does not store column values in any dictionary view, because it is non sense. Oracle 不在任何字典视图中存储列值,因为它没有意义。 Values are stored in the corresponding tables.值存储在相应的表中。

So, a way to do that could be using PL/SQL, but it will take a long time if your database is big.因此,一种方法可能是使用 PL/SQL,但如果您的数据库很大,则需要很长时间。 In my example, I am looking for a string which I can limit by only applying to columns where the data type is varchar2 or char, and as I am looking for a specific value, I discard all columns which its length is lower than the value I am looking for.在我的示例中,我正在寻找一个字符串,我可以通过仅应用于数据类型为 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;
/

An example where I limited the tables to look for我限制要查找的表的示例

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.

相关问题 如何从多个表中选择数据,其中表名是另一个表的列中的值? - How to select data from multiple tables where table name is value from a column of another table? 从具有特定表名的所有表中选择所有值 - Select all values from all tables with specific table name 如何遍历具有特定名称的所有表,然后更新每个表中的特定列值 - How can i traverse all the tables with specific name and then update a particular column value from each table 从3列表中选择所有记录,其中2个特定列中有重复项 - Select all records from a 3 column table where there is duplication in 2 specific columns 从表中选择列值具有“最高优先级”的行 - Select row from table where column value has "highest priority" 如何从oracle架构中包含该列名的所有表表中获取特定列的最大值? - How to get the max value for a specific column from all tables tables which contains that column name in an oracle schema? 如何选择任何列具有特定值的sql数据库表中的所有记录 - How to select all records in sql database tables where any column has a particular value 从两个表中选择所有列,按表1中的所有列和表2中的特定列分组 - Select all columns from two tables grouping by all columns in table1 and specific column in table2 查找具有特定列名 + 值小于 X 的所有表 - Find all tables with a specific column name + where value is lower than X 从表中选择column_name,其中column_name = all_values - select column_name from table where column_name = all_values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM