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
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 //
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:
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…