2011-01-06 141 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