我创建了一个存储过程,因为我希望有一个场景,我想从数据库中获取下一个数字。
DELIMITER $$
CREATE PROCEDURE `INCREMENT_IDENTIFIERS`(IN key_name varchar(30))
BEGIN
SET @keyName := key_name;
UPDATE APS_IDENTIFIERS SET COUNTER = COUNTER + 1 WHERE KEY_NAME = @keyName;
SELECT * FROM APS_IDENTIFIERS WHERE KEY_NAME = @keyName;
END$$
DELIMITER ;
初始列值
调用存储过程后的最终列值
call INCREMENT_IDENTIFIERS('urn');
现在我的问题是,即使我传递了完全匹配的输入条件,这个存储过程为什么会增加两行,并且它会以增量值的形式返回两行作为输出。
答案 0 :(得分:1)
没有必要使用9.4 User-Defined Variables,可以使用输入参数或13.6.4.1 Local Variable DECLARE Syntax:
mysql> DELIMITER //
mysql> CREATE PROCEDURE `INCREMENT_IDENTIFIERS`(
-> `in_key_name` VARCHAR(30)
-> )
-> BEGIN
-> UPDATE `APS_IDENTIFIERS`
-> SET `COUNTER` = `COUNTER` + 1
-> WHERE `KEY_NAME` = `in_key_name`;
-> SELECT `KEY_NAME`, `COUNTER`
-> FROM `APS_IDENTIFIERS`
-> WHERE `KEY_NAME` = `in_key_name`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `INCREMENT_IDENTIFIERS`('urn');
+----------+---------+
| KEY_NAME | COUNTER |
+----------+---------+
| urn | 3 |
+----------+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
请参阅db-fiddle。
<强>更新强>
避免使用列名作为参数名称:
mysql> DELIMITER //
mysql> CREATE PROCEDURE `INCREMENT_IDENTIFIERS`(
-> `key_name` VARCHAR(30) -- CHANGE NAME
-> )
-> BEGIN
-> SET @`keyName` := `key_name`;
-> UPDATE `APS_IDENTIFIERS`
-> SET `COUNTER` = `COUNTER` + 1
-> WHERE `KEY_NAME` = @`keyName`;
-> SELECT `KEY_NAME`, `COUNTER`
-> FROM `APS_IDENTIFIERS`
-> WHERE `KEY_NAME` = @`keyName`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `INCREMENT_IDENTIFIERS`('urn');
+------------+---------+
| `KEY_NAME` | COUNTER |
+------------+---------+
| urn | 69 |
| urn | 3 |
+------------+---------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
新参数名称:
mysql> DELIMITER //
mysql> CREATE PROCEDURE `INCREMENT_IDENTIFIERS`(
-> `_key_name` VARCHAR(30) -- NEW NAME
-> )
-> BEGIN
-> SET @`keyName` := `_key_name`;
-> UPDATE `APS_IDENTIFIERS`
-> SET `COUNTER` = `COUNTER` + 1
-> WHERE `KEY_NAME` = @`keyName`;
-> SELECT `KEY_NAME`, `COUNTER`
-> FROM `APS_IDENTIFIERS`
-> WHERE `KEY_NAME` = @`keyName`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `INCREMENT_IDENTIFIERS`('urn');
+----------+---------+
| KEY_NAME | COUNTER |
+----------+---------+
| urn | 3 |
+----------+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
请参阅db-fiddle。
检查文档13.6.4.2 Local Variable Scope and Resolution。
更新2
另一个选项(不太推荐)是使用合格的列名(APS_IDENTIFIERS.KEY_NAME
):
mysql> DELIMITER //
mysql> CREATE PROCEDURE `INCREMENT_IDENTIFIERS`(
-> `key_name` VARCHAR(30)
-> )
-> BEGIN
-> SET @`keyName` := `key_name`;
-> UPDATE `APS_IDENTIFIERS`
-> SET `COUNTER` = `COUNTER` + 1
-> WHERE `APS_IDENTIFIERS`.`KEY_NAME` = @`keyName`;
-> SELECT `KEY_NAME`, `COUNTER`
-> FROM `APS_IDENTIFIERS`
-> WHERE `APS_IDENTIFIERS`.`KEY_NAME` = @`keyName`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `INCREMENT_IDENTIFIERS`('urn');
+------------+---------+
| `KEY_NAME` | COUNTER |
+------------+---------+
| urn | 3 |
+------------+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
请参阅db-fiddle。