【问题标题】:Updating a column matching a specific pattern in all table in an oracle database在 oracle 数据库的所有表中更新与特定模式匹配的列
【发布时间】:2014-08-28 08:09:13
【问题描述】:

我需要更新与 oracle 数据库中所有表中的特定模式匹配的列。 例如,我在所有表中都有此列 *_CID 是主表的外键,女巫具有主键 CID

谢谢

【问题讨论】:

    标签: sql database oracle oracle-sqldeveloper oracle12c


    【解决方案1】:

    可以使用命名约定查询all_tab_columns

    declare
        cursor c is 
            select table_owner , column_name, table_name from all_tab_columns where column_name like '%_CID';
    begin 
        for x in c loop
            execute immediate 'update ' || x.table_owner || '.' || x.table_name ||' set ' || x.column_name||' = 0';
        end loop;
    end;
    

    如果您有有效的 Fk,您还可以使用 all_tab_constraints 为您的主表启用 fetch 的 FK 并获取 r_constraint_name 的列名称。

    【讨论】:

    • 问题是我不知道我是否有有效的 FK
    【解决方案2】:

    我找到了解决问题的方法:

    BEGIN  
      FOR x IN (SELECT owner, table_name, column_name FROM all_tab_columns) LOOP   
        EXECUTE IMMEDIATE  'update ' || x.owner || '.' || x.table_name ||' set ' || x.column_name||' = 0 where '||x.column_name||' = 1';
      END LOOP;
    END;
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-14
      • 2014-02-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多