0

我的表结构为:

 tabble_name_temp1


    col_up      col_down

    akash      kavita 

    kavita     aruhi

    aruhi      Akash



 tabble_name_temp2


    col_up      col_down

    akash      kavita 

    kavita     simran

    jyoti     Akash


   tabble_name_temp3


    col_up      col_down

    akash      kavita 

    kavita     simran

    jyoti     Akash

我想选择表的名称

值,例如:如果我输入

        akash and simran

那么这应该像

       tabble_name_temp2

       tabble_name_temp3

提前致谢!

4

1 回答 1

1

Oracle 有all_objects存储all_tab_columns表/列相关数据的表。您可以从这些表中进行选择,然后形成动态查询来检查给定的数据是否存在于这些表中。这是单个输入的示例代码。如果有多个输入,您将不得不多次重复此操作。

DECLARE
   l_chr_query   VARCHAR2 (2000);
   l_num_count   NUMBER (1);
BEGIN
   FOR i IN (SELECT table_name, column_name
               FROM all_tab_columns
              WHERE table_name IN (
                       SELECT object_name
                         FROM all_objects
                        WHERE object_name LIKE 'SA_%' AND object_type = 'TABLE')
                AND data_type = 'VARCHAR2') --modify the where clause as per your needs
    LOOP
      l_num_count := 0;
      l_chr_query :=
            'select count(1) from ' || i.table_name || ' where ' || i.column_name
         || ' = ''akash'' and rownum = 1';

      --execute the query
      EXECUTE IMMEDIATE l_chr_query INTO l_num_count;

      IF l_num_count > 0
      THEN
         DBMS_OUTPUT.put_line (i.table_name);
      END IF;
   END LOOP;
EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (SQLERRM);
END;

找到表后,您可以将其保存在数组中,并在同一个表但不同列的下一次迭代中跳过检查同一个表。

于 2013-05-24T09:26:20.477 回答