在MySQL存储过程中实现DELETE查询
您可以使用存储过程,并可以通过参数传递值。让我们首先创建一个表-
mysql> create table DemoTable1464 -> ( -> Id int, -> FirstName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1464 values(101,'Chris Brown'); mysql> insert into DemoTable1464 values(102,'John Doe');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1464;
这将产生以下输出-
+------+-------------+ | Id | FirstName | +------+-------------+ | 101 | Chris Brown | | 102 | John Doe | +------+-------------+ 2 rows in set (0.00 sec)
这是在存储过程中实现删除查询的查询-
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 // mysql> DELIMITER ;
现在您可以使用CALL命令调用存储过程-
mysql> call delete_demo(102,'John Doe');
让我们再次检查表记录-
mysql> select * from DemoTable1464;
这将产生以下输出-
+------+-------------+ | Id | FirstName | +------+-------------+ | 101 | Chris Brown | +------+-------------+ 1 row in set (0.00 sec)