Implement DELETE query in MySQL stored procedure


You can use stored procedure and can pass the value via parameter. Let us first create a table −

mysql> create table DemoTable1464
   -> (
   -> Id int,
   -> FirstName varchar(20)
   -> );
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1464 values(101,'Chris Brown');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1464 values(102,'John Doe');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1464;

This will produce the following output −

+------+-------------+
| Id   | FirstName   |
+------+-------------+
|  101 | Chris Brown |
|  102 | John Doe    |
+------+-------------+
2 rows in set (0.00 sec)

Here is the query to implement delete query in stored procedure −

mysql> DELIMITER //
mysql> CREATE PROCEDURE delete_demo(IN local_Id int, IN local_Name varchar(20))
   -> BEGIN
   ->    delete from DemoTable1464
   ->    where Id=local_Id and
   ->      FirstName=local_Name;
   -> END //
Query OK, 0 rows affected (0.24 sec)
mysql> DELIMITER ;

Now you can call the stored procedure using CALL command −

mysql> call delete_demo(102,'John Doe');
Query OK, 1 row affected, 1 warning (0.11 sec)

Let us check the table records once again −

mysql> select * from DemoTable1464;

This will produce the following output −

+------+-------------+
| Id   | FirstName   |
+------+-------------+
|  101 | Chris Brown |
+------+-------------+
1 row in set (0.00 sec)

Updated on: 10-Dec-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements