从所有表中删除特定值列

时间:2016-06-08 21:44:14

标签: sql oracle

要调用我正在使用的所有表格:

select * from cat

删除一个值,例如customer_nr = 10000我正在使用:

delete from customers where customer_nr = 10000;

但是如何从所有表中删除此特定值?

1 个答案:

答案 0 :(得分:2)

  

我正在寻找像delete from "all tables" where customer_nr = 10000;

这样的东西

您无法使用单个SQL语句从所有表中删除。

最安全的方法是分别显式删除每个表中的所有项目:

DELETE FROM customers WHERE customer_nr = 10000
DELETE FROM other_table1 WHERE customer_nr = 10000
DELETE FROM other_table2 WHERE customer_nr = 10000

你可以用动态SQL来做(但请不要 - 有一天它会出错,你会删除你不想要的值。)

类似的东西:

DECLARE
  cmds SYS.ODCIVARCHAR2LIST;
BEGIN
  SELECT 'DELETE FROM ' || owner || '.' || table_name || ' WHERE customer_nr = 10000'
  BULK COLLECT INTO cmds
  FROM all_tab_columns
  WHERE column_name = 'CUSTOMER_NR';

  FOR i IN 1 .. cmds.COUNT LOOP
    -- DBMS_OUTPUT.PUT_LINE( cmds(i) );
    EXECUTE IMMEDIATE cmds(i);
  END LOOP;
END;
/

但请 - 不要。