不幸的是,MySQL不支持DEFAULT
参数值,因此:
CREATE PROCEDURE `blah`
(
myDefaultParam int DEFAULT 0
)
BEGIN
END
返回错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'DEFAULT 0) BEGIN END' at line 3
要解决此限制,只需创建将默认值分配给原始过程的其他过程即可:
DELIMITER //
DROP PROCEDURE IF EXISTS blah//
DROP PROCEDURE IF EXISTS blah2//
DROP PROCEDURE IF EXISTS blah1//
DROP PROCEDURE IF EXISTS blah0//
CREATE PROCEDURE blah(param1 INT UNSIGNED, param2 INT UNSIGNED)
BEGIN
SELECT param1, param2;
END;
//
CREATE PROCEDURE blah2(param1 INT UNSIGNED, param2 INT UNSIGNED)
BEGIN
CALL blah(param1, param2);
END;
//
CREATE PROCEDURE blah1(param1 INT UNSIGNED)
BEGIN
CALL blah2(param1, 3);
END;
//
CREATE PROCEDURE blah0()
BEGIN
CALL blah1(4);
END;
//
然后,运行此命令:
CALL blah(1, 1);
CALL blah2(2, 2);
CALL blah1(3);
CALL blah0();
将返回:
+
| param1 | param2 |
+
| 1 | 1 |
+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+
| param1 | param2 |
+
| 2 | 2 |
+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+
| param1 | param2 |
+
| 3 | 3 |
+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+
| param1 | param2 |
+
| 4 | 3 |
+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
然后,如果您确保只使用blah2()
,blah1()
和blah0()
程序,你的代码将不再需要立即更新,当您添加第三个参数blah()
的过程。