执行删除操作的过程

时间:2012-06-14 12:29:32

标签: sql stored-procedures mybatis

我想编写一个程序来执行以下

的删除
i want to delete from x_table and y_table where id=select id from z_table where obj_id="1234" also at the same time i want to delete 
from user_table id = select id from z_table where obj_id="1234" and user_id != in (select user id from main_table)

in short i want to delete the rows from 3 tables x_table,y_table and user_table for the given obj_id and for user_table i want to alsocheck if user_id is not in other main_table

1 个答案:

答案 0 :(得分:1)

试试这个过程。请注意,这不是一个记事本认证的版本:)我没有在Oracle上编译它,因为我没有在我的个人计算机上。看它是否有效..

CREATE
OR REPLACE PROCEDURE testproc(inputVar IN number)

AS

BEGIN

EXECUTE IMMEDIATE 'DELETE FROM TABLE_X WHERE ID = (SELECT ID FROM TABLE_Z WHERE OBJ_ID=:num)' USING IN inputVar;

EXECUTE IMMEDIATE 'DELETE FROM TABLE_Y WHERE ID = (SELECT ID FROM TABLE_Z WHERE OBJ_ID=:num)' USING IN inputVar;

EXECUTE IMMEDIATE 'DELETE FROM TABLE_USER WHERE ID = (SELECT ID FROM TABLE_Z WHERE OBJ_ID=:num) AND USER_ID NOT IN (SELECT USER_ID FROM MAIN_TABLE)' USING IN inputVar;

COMMIT;

EXCEPTION

WHEN
OTHERS

THEN

dbms_output.put_line
(SQLERRM);

END;