如何使用 PL/SQL 从不同的表中删除(删除)多个用户

How to remove(delete) multiple users from different tables using PL/SQL

我编写此查询是为了使用 pl/sql 从不同的表中删除一个用户。

示例:我运行此查询删除一个用户:

用户 SPIKETJ, code 01234, code_id 85412974l_code_user SPIKETJ

declare 
  l_code_name  table_2.cod_name%type;
  l_code       table_2.cod_emp%type;
  l_code_id    table_0.cod_id%type;
  l_code_user  table_03.cod_user%type;    
begin
  l_code_name := 'SPIKETJ';
  l_code := '01234';
  l_code_id := '85412974';
  l_code_user := 'SPIKETJ';

  DELETE table_2 WHERE cod_emp  IN (l_code);
  commit;

  DELETE table_65 WHERE cod_emp IN (l_code);
  commit;

  DELETE table_41 WHERE cod_name IN (l_code_name);
  commit;

  DELETE table_18 WHERE cod_name IN (l_code_name);
  commit;

  DELETE table08 WHERE cod_user IN (l_code_name);
  commit;

  DELETE table_0 WHERE cod_docum IN (l_code_id);
  commit;

  DELETE table_17 WHERE cod_id IN (l_code_id);
  commit;

  DELETE table_03 WHERE cod_user IN (l_code_user);
  commit;

END;

当我必须删除一个用户时,我只 change/assign 值: l_code_name, l_code, l_code_id, l_code_user.

但是现在,我要删除将近20个用户! 所以我想知道我是否必须 运行 这个查询 20 次,每次都更改变量值?

我可以写一个 query/block,其中 运行ning 一次删除我想要的 20 个用户吗?

您可以 Create procedure 按照 Tony Andrews 上面的建议。

程序

Create or replace Procedure Delete_user
(l_code_name IN your_users.cod_name%type,    --   Declare your IN parameters here
l_code       IN your_users.cod_emp%type,
l_code_id    IN your_users.cod_id%type,
l_code_user  IN your_users.cod_user%type
)
AS
--   Declare your local variables
v_code_name your_users.cod_name%type := l_code_name;    
v_code      your_users.cod_emp%type  := l_code;
v_code_id   your_users.cod_id%type   := l_code_id ;
v_code_user your_users.cod_user%type := l_code_user;
BEGIN
--- write your code(delete statements)
DELETE from your_users
WHERE cod_emp  IN (v_code);
commit;
dbms_output.put_line( 'USER : ' || ' ' ||  v_code_user || ' is deleted.' );
---
--- similarly other delete statements
END DELETE_USER;

输出:

Procedure created.

使用以下命令检查错误:

显示错误;

没有错误

调用你的删除用户程序:

 BEGIN
 DELETE_USER('SPIKETJ',01234,85412974,'SPIKETJ');
 DELETE_USER('JACKET',99999,111111,'JACKET');
 --similary add other user details in order of the parameters declared in proc
 END;

输出:

USER :  SPIKETJ is deleted.
USER :  JACKET is deleted.

Statement processed.

在此处阅读更多内容 Procedures

用于删除用户而不是调用过程 N (20) 次。

Create or replace Procedure Delete_user
 AS 
v_code_id   your_users.cod_id%type;  
v_code_user your_users.cod_user%type ;
cursor C_users is select cod_id,cod_user from your_users
 where 1=1;  -- add condition to select users you wish to delete   

BEGIN
OPEN C_users;
loop
Fetch C_users into v_code_id,v_code_user;
exit when C_users%NOTFOUND;
DELETE from your_users WHERE cod_emp  IN (v_code_id); -- use your primary key 
--- write delete statements for other tables
dbms_output.put_line( 'USER : ' || ' ' ||  v_code_user || ' is deleted.' );
End Loop;
commit;
Close C_users ;

END DELETE_USER;

 -- Procedure created.

输出:

USER :  mahi is deleted.
USER :  xyz is deleted.

Statement processed.