如何在匹配条件时显示数据库中的表名

时间:2013-05-24 05:13:39

标签: php mysql database oracle11g

我的表结构如下:

 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

提前致谢!

1 个答案:

答案 0 :(得分:1)

Oracle有all_objectsall_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;

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