2011-01-06 149 views
0

我已經創建了一個存儲過程來刪除多個表中的數據。我的工作流程如下存儲過程+ mysql問題

我使用的MySQL 5.0和Linux上運行

表的依賴關係取決於表B.根據表 B表如下

表C A

我想刪除表A中的記錄並刪除表B和表C中的所有相關記錄

1-刪除詳細表(C)中的所有數據(使用存儲過程sp_delete_from_C)

2 - 刪除相關的數據直接子表(B)(與存儲過程sp_delete_from_B)

3 - 刪除主表(A)(與存儲過程sp_delete_from_A)

我已經寫了下面的程序

CREATE PROCEDURE sp_A_rollback(IN aId INT UNSIGNED) 
READS SQL DATA 
BEGIN 
DECLARE b_id INT DEFAULT 0; 

DECLARE cur_1 CURSOR FOR SELECT id FROM b where a_id=aId; 

OPEN cur_1; 
read_loop: LOOP 
    FETCH cur_1 INTO a_id; 
    CALL sp_delete_from_C(b_id); 
END LOOP; 
CLOSE cur_1; 

CALL sp_delete_from_B(aId); 
CALL sp_delete_from_A(aId); 


END // 

我的問題是,

如果我單獨運行這些程序,它的工作原理

但如果你運行sp_A_rollback它只執行'sp_delete_from_C'

我沒有任何想法爲什麼它沒有調用其他2 sps。我是mysql存儲過程的新手。請有人可以幫助我

在此先感謝

sameera

回答

2

我不知道爲什麼你使用遊標 - 所有你需要的是類似以下內容:

drop procedure if exists cascade_delete_tableA; 
delimiter # 

create procedure cascade_delete_tableA 
(
in p_id int unsigned 
) 
begin 

delete from tableC where a_id = p_id; 
delete from tableB where a_id = p_id; 
delete from tableA where id = p_id; 

end# 

delimiter ; 

通話存儲過程從應用程序代碼中包裝在一個事務中。

編輯

你需要使用連接到您的表C刪除行。這是一個更全面的例子,供你學習http://pastie.org/1435521。另外,你的遊標循環不會讀入正確的變量,這就是爲什麼它不能以當前的形式工作。我仍然建議你檢查以下...

-- TABLES 

drop table if exists customers; 
create table customers 
(
cust_id smallint unsigned not null auto_increment primary key, 
name varchar(255) not null 
) 
engine=innodb; 

drop table if exists orders; 
create table orders 
(
order_id int unsigned not null auto_increment primary key, 
cust_id smallint unsigned not null 
) 
engine=innodb; 

drop table if exists order_items; 
create table order_items 
(
order_id int unsigned not null, 
prod_id smallint unsigned not null, 
primary key (order_id, prod_id) 
) 
engine=innodb; 

-- STORED PROCS 

drop procedure if exists cascade_delete_customer; 
delimiter # 

create procedure cascade_delete_customer 
(
in p_cust_id smallint unsigned 
) 
begin 

declare rows int unsigned default 0; 

-- delete order items 

delete oi from order_items oi 
inner join orders o on o.order_id = oi.order_id and o.cust_id = p_cust_id; 

set rows = row_count(); 

-- delete orders 

delete from orders where cust_id = p_cust_id; 

set rows = rows + row_count(); 

-- delete customer 

delete from customers where cust_id = p_cust_id; 

select rows + row_count() as rows; 

end# 

delimiter ; 

-- TEST DATA 

insert into customers (name) values ('c1'),('c2'),('c3'),('c4'); 

insert into orders (cust_id) values (1),(2),(3),(1),(1),(3),(2),(4); 

insert into order_items (order_id, prod_id) values 
(1,1),(1,2),(1,3), 
(2,5), 
(3,2),(3,5),(3,8), 
(4,1),(4,4), 
(5,2),(5,7), 
(6,4),(6,8),(6,9), 
(7,5), 
(8,3),(8,4),(8,5),(8,6); 

-- TESTING 

/* 

select * from customers where cust_id = 1; 

select * from orders where cust_id = 1; 

select * from order_items oi 
inner join orders o on oi.order_id = o.order_id and o.cust_id = 1; 

call cascade_delete_customer(1); 

*/ 
+0

嗨f00,感謝您的答案。我在這裏使用遊標,因爲從表C中刪除我只有表B的關鍵。因爲我需要一個循環,我使用遊標。在真正的renario我不得不使用其他存儲過程,而不是直接刪除語句(因爲涉及到一些邏輯和這些過程已經實施)。所以我必須調用這個SP內的其他sps。任何想法,再次感謝您的答案 – sameera207 2011-01-06 19:55:30