我写了这个查询,用pl / sql从不同的表中删除一个用户。
示例:我运行此查询以删除一个用户:
用户 SPIKETJ
,代码 01234
, code_id 85412974
和 l_code_user < / strong> 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;
当我必须删除一个用户时,我只更改/分配值:
l_code_name
,l_code
,l_code_id
,l_code_user
。
但是现在,我必须删除近20个用户! 所以我想知道每次更改变量值是否必须运行此查询20次?
或
我可以编写一个查询/块,运行一次会删除我想要的20个用户吗?
答案 0 :(得分:1)
你可以按照Tony Andrews的建议Create procedure。
程序
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
答案 1 :(得分:1)
删除用户而不是调用过程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.