Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3635210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:52:31+00:00 2026-05-19T00:52:31+00:00

I have created a stored procedure to delete data from multiple tables. my work

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:52:31+00:00Added an answer on May 19, 2026 at 12:52 am

    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);
    
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
Suppose I have a stored procedure that manages its own transaction CREATE PROCEDURE theProc
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a C# class file by using a XSD-file as an input.
I have the following Query and i need the query to fetch data from
I have monthly sales figures stored in separate sheets. I would like to create
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up
I have created a PHP-script to update a web server that is live inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.