oracle 如何在所有表中搜索一个值[重复]

jaxagkaj  于 2023-08-03  发布在  Oracle
关注(0)|答案(1)|浏览(106)

此问题在此处已有答案

Search All Fields In All Tables For A Specific Value (Oracle)(17回答)
11天前关闭。
我有一个超过100个表的数据库。我想在此数据库的表中查找包含'ATMOSPHERIC'数据的表。我该怎么做。我自己写了一个这样的代码,但它没有工作。我在等你的建议。

DECLARE
  v_search_term VARCHAR2(100) := 'ATMOSFERIK';
  v_sql         VARCHAR2(4000);
  v_result      NUMBER;
BEGIN
  FOR t IN (SELECT *
            FROM all_tab_columns
            WHERE data_type LIKE '%CHAR%' OR data_type LIKE '%CLOB%'
            ORDER BY table_name, column_name)
  LOOP
    v_sql := 'SELECT COUNT(*) FROM myDB.' || t.table_name ||
             ' WHERE ' || t.column_name || ' LIKE ''%' || v_search_term || '%''';
    EXECUTE IMMEDIATE v_sql INTO v_result;

    IF v_result > 0 THEN
      DBMS_OUTPUT.PUT_LINE('Table: SMS.' || t.table_name || ', Column: ' || t.column_name);
    END IF;
  END LOOP;
END;

字符串
编辑:错误报告- ORA-00942:表或视图不存在ORA-06512:在1300942线。00000 -“表或视图不存在”* 原因:

  • 行动:
p4rjhz4m

p4rjhz4m1#

对所有者使用带引号的标识符和筛选器:

DECLARE
  v_owner       VARCHAR2(30)  := 'MYDB';
  v_search_term VARCHAR2(100) := 'ATMOSFERIK';
  v_sql         VARCHAR2(4000);
  v_result      NUMBER;
BEGIN
  FOR t IN (
    SELECT owner, table_name, column_name
    FROM   all_tab_columns
    WHERE (data_type LIKE '%CHAR%' OR data_type LIKE '%CLOB%')
    AND    owner = v_owner
    ORDER BY owner, table_name, column_name
  )
  LOOP
    v_sql := 'SELECT COUNT(*)'
          || ' FROM   "' || t.owner || '"."' || t.table_name || '"'
          || ' WHERE  "' || t.column_name || '" LIKE ''%' || v_search_term || '%''';
    EXECUTE IMMEDIATE v_sql INTO v_result;

    IF v_result > 0 THEN
      DBMS_OUTPUT.PUT_LINE('Table: SMS.' || t.table_name || ', Column: ' || t.column_name);
    END IF;
  END LOOP;
END;
/

字符串
如果你有样本数据:

CREATE TABLE mydb.table_name (a, b, c, d) AS
SELECT 'x', DATE '2023-01-01', 42, 'ATMOSFERIK' FROM DUAL;

CREATE TABLE mydb."lower_case_table_name" ("a", "b", "c", "d") AS
SELECT 'x', DATE '2023-01-01', 'ATMOSFERIK', 42 FROM DUAL;


则输出为:

Table: SMS.TABLE_NAME, Column: D
Table: SMS.lower_case_table_name, Column: c


fiddle

相关问题