简体   繁体   中英

How to write a stored procedure in mysql to delete records from entire tables in database using MySQL?

I have 4 tables joined together in MySQL and now i need to write a stored procedures to delete a record from all tables by passing ID and database name from procedure parameters dynamically. while i call my procedures it's like the below code:

CALL delete_case_fromTables('databasename',1);

I wrote a procedure as below, but its not what i need, but i need to delete a record from all 4 tables in a database dynamically.

DELIMITER $$
CREATE PROCEDURE delete_cms_case_demo(IN DATABASE_NAME VARCHAR(200),IN CASE_URN INT)

BEGIN
    SET @sql = CONCAT('DELETE FROM ', DATABASE_NAME,'.appellateinv WHERE case_urn= ', CASE_URN);
    PREPARE stmt FROM @SQL;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

END$$

Please Help me in this regard. Thanks

Good news (possibly) procedure is fine.

    drop table if exists cascade2,cascade3;
    drop table if exists cascade1;

    create table cascade1 (case_urn int primary key, msg varchar(10));
    create table cascade2 (case_urn int , msg varchar(10));
    create table cascade3 (case_urn int , msg varchar(10));

    alter table cascade2
        add foreign key fk2(case_urn) references cascade1(case_urn) on delete cascade;
    alter table cascade3
        add foreign key fk3(case_urn) references cascade1(case_urn) on delete cascade;

    insert into cascade1 values (1,'aaa'),(2,'bbb'),(3,'ccc');  
    insert into cascade2 values (1,'aaa'),(2,'bbb'),(3,'ccc');  
    insert into cascade3 values (2,'bbb'),(3,'ccc');    


    call p_delete_cascade('sandbox',1);
    select 'c1', case_urn , msg from cascade1
    union all
    select 'c2', case_urn , msg from cascade2
    union all
    select 'c3', case_urn , msg from cascade3   

procedure
drop procedure if exists p_delete_cascade;
DELIMITER $$
CREATE PROCEDURE p_delete_cascade(IN DATABASE_NAME VARCHAR(200),IN CASE_URN INT)

BEGIN
    SET @sql = CONCAT('DELETE FROM ', database_name,'.cascade1 WHERE case_urn = ', Case_urn);
    PREPARE stmt FROM @SQL;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

END$$

delimiter ;

Result

+----+----------+------+
| c1 | case_urn | msg  |
+----+----------+------+
| c1 |        2 | bbb  |
| c1 |        3 | ccc  |
| c2 |        2 | bbb  |
| c2 |        3 | ccc  |
| c3 |        2 | bbb  |
| c3 |        3 | ccc  |
+----+----------+------+
6 rows in set (0.00 sec)

To delete on a shared column name

drop procedure if exists p_delete_on_shared_col_name;
DELIMITER $$
CREATE PROCEDURE p_delete_on_shared_col_name(IN DATABASE_NAME VARCHAR(200),IN CASE_URN INT)

BEGIN

     declare tablenames varchar(1000);
     declare tname varchar(100);

     select group_concat(table_name) 
     into tablenames
     from information_schema.`COLUMNS` 
     where column_name = 'case_urn'
      ;

      #insert into debug_table(msg) values (tablenames);

     while instr(tablenames,',') > 0 do
        set tname = substring(tablenames,1,instr(tablenames,',') -1);
        set tablenames = replace(tablenames,concat(tname,','),'');
        SET @sql = CONCAT('DELETE FROM ', database_name,'.',tname ,' WHERE case_urn = ', Case_urn);
        PREPARE stmt FROM @SQL;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        #insert into debug_table(msg) values (@sql);
     end while ;
     set tname = tablenames;
     SET @sql = CONCAT('DELETE FROM ', database_name,'.',tname ,' WHERE case_urn = ', Case_urn);
     #insert into debug_table(msg) values (@sql);

     PREPARE stmt FROM @SQL;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

END $$

delimiter ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM