我在所有数据库表中都有CREATED_BY列。我想删除CREATED_BY =' xyz'行进的所有表格中的行列值。不是编写单独的DELETE查询,而是有一种简单的方法来实现这个oracle数据库。
答案 0 :(得分:1)
您可以循环浏览user_tab_columns
并在EXECUTE IMMEDIATE
中运行删除。在最终代码中使用适当的例外。
SET SERVEROUTPUT ON
BEGIN
FOR t IN (SELECT TABLE_NAME
FROM USER_TAB_COLUMNS
WHERE COLUMN_NAME = 'CREATED_BY')
LOOP
DBMS_OUTPUT.PUT_LINE (
'DELETE FROM ' || t.TABLE_NAME || ' WHERE CREATED_BY = ''xyz''');
EXECUTE IMMEDIATE
'DELETE FROM ' || t.TABLE_NAME || ' WHERE CREATED_BY = ''xyz''';
END LOOP;
END;
答案 1 :(得分:0)
您需要Procedure
,如下所示。这将输入'xyz'
作为输入并删除所有具有created_by = 'xyz'
的表格中的所有行
CREATE OR REPLACE PROCEDURE del_row (usrnm VARCHAR2)
AS
TYPE var IS TABLE OF VARCHAR2 (100)
INDEX BY PLS_INTEGER;
v_var var;
v_sql VARCHAR2 (1000);
BEGIN
SELECT tname
BULK COLLECT INTO v_var
FROM col
WHERE cname = 'CREATED_BY';
FOR i IN 1 .. v_var.COUNT
LOOP
v_sql := 'Delete from ' || v_var (i) || ' where CREATED_BY like ''%'||usrnm||'%''';
EXECUTE IMMEDIATE v_sql ;
END LOOP;
COMMIT;
END;
演示:
CREATE TABLE testt
(
col1 VARCHAR2 (1),
created_by VARCHAR2 (3)
);
SQL> SELECT * FROM testt;
C CRE
- ---
A XYZ
B XYZ
SQL> EXEC del_row('XYZ');
PL/SQL procedure successfully completed.
SQL> SELECT * FROM testt;
no rows selected
答案 2 :(得分:-1)
试试这个
DELETE (SELECT *
FROM tab1
INNER JOIN tab2
ON tab1.id= tab2.id
WHERE tab1.id >= any_value
)
它将从两个表中删除数据。