stored procedure + mysql question
I have created a stored procedure to delete data from multiple tables. my work flow as follows
I'm using mysql 5.0 and running on linux
table dependencies as follows
table C depending on table B table B depending on table A
I want to delete a record in table A and delete all the related records in tables B and C
1 - delete all the data from detail tables (C) (with stored procedure sp_delete_from_C)
2 - delete related data immediate child table (B) (with stored procedure sp_delete_from_B)
3 - delete master table (A) (with stored procedure sp_delete_from_A)
I have wrote the following procedure
CR开发者_开发技巧EATE 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 //
My question is,
If i run these procedures individually it works
but if u run sp_A_rollback it execute only 'sp_delete_from_C'
I dont have any idea why its not calling the other 2 sps. I'm a newbee to mysql stored procedures. please can someone help me
thanks in advance
sameera
I have no idea why you're using cursors - all you need is something like the following:
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 ;
call the stored procedure from your application code wrapped up in a transaction.
EDIT
You'll need to use a join to delete rows from your tableC. Here's a more comprehensive example for you to study http://pastie.org/1435521. Also, your cursor loop isnt fetching into the correct variable which is why it's not working in it's current form. I would still recommend you examine the following...
-- 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);
*/
精彩评论