2012-06-14 71 views
0

我想寫一個程序,執行以下程序執行刪除操作

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

一個刪除試試這個PROC。請不要這是一個記事本認證的版本:)我沒有編譯它在甲骨文,因爲我沒有在我的個人計算機上。看看它的工作原理..

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;