我们将创建一个存储过程,无论何时更新变量值,该过程都不会更改。
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Value) values(100); Display all records from the table using select statement : mysql> select *from DemoTable;
输出结果
+----+-------+ | Id | Value | +----+-------+ | 1 | 100 | +----+-------+ 1 row in set (0.00 sec)
以下是存储过程,显示了更新后的旧值-
mysql> DELIMITER // mysql> CREATE PROCEDURE updateValue100() BEGIN DECLARE myValue int; select @myValue :=(select Value from DemoTable where Id=1); select @myValue; update DemoTable set Value=200 where Id=1; select @myValue :=(select Value from DemoTable where Id=1); select @myValue; END // mysql> DELIMITER ;
现在您可以使用CALL命令调用存储过程-
mysql> call updateValue100();
输出结果
+-------------------------------------------------------+ | @myValue :=(select Value from DemoTable where Id=1) | +-------------------------------------------------------+ | 100 | +-------------------------------------------------------+ 1 row in set (0.00 sec) +----------+ | @myValue | +----------+ | 100 | +----------+ 1 row in set (0.01 sec) +-------------------------------------------------------+ | @myValue :=(select Value from DemoTable where Id=1) | +-------------------------------------------------------+ | 200 | +-------------------------------------------------------+ 1 row in set (0.16 sec) +----------+ | @myValue | +----------+ | 200 | +----------+ 1 row in set (0.17 sec)