如何在不声明所有参数的情况下在 mySQL 中创建存储过程?

How can I create a stored procedure in mySQL without declaring all the parameters?

可能是这个问题的简单答案,但我无法在网上找到答案:(

我目前正在迁移到 MySQL 数据库,但是我在从 Visual Studio 中的数据集中传输我的存储过程时遇到问题。我数据集中的 SQL 查询并没有强制我命名所有参数,但是, MySQL 在我使用时出现语法错误?而不是命名和声明每个参数。

SQL 在 Visual Studio 的数据集中查询:

UPDATE       tblConfigurations
SET                DEUnit_ID_UnitName = ?, DEUnit_ID_UnitType = ?, DEUnit_ID_UnitLevel = ?, DEUnit_Aut_NumberOfMastercards = ?, DEUnit_Aut_MasterCardId1 = ?, DEUnit_Aut_MasterCardId2 = ?, 
                         DEUnit_Aut_MasterCardId3 = ?, DEUnit_Aut_MasterCardId4 = ?, DEUnit_Aut_MasterCardId5 = ?, DEUnit_Aut_MasterCardId6 = ?, DEUnit_Aut_MasterCardId7 = ?, DEUnit_Aut_MasterCardId8 = ?, 
                         DEUnit_Lck_SeperatedAntennas = ?, DEUnit_AF_SilentAlarmActivation = ?, DEUnit_AF_AlarmInputConnection = ?, DEUnit_NW_Address = ?, DEUnit_KD_KeyboardOrientation = ?, 
                         DEUnit_KD_SpecialAcousticConfirmation = ?, DEUnit_KD_KeyClickSound = ?, DEUnit_KD_LedColors = ?, DEUnit_ReservedValue1 = ?, DEUnit_ReservedValue2 = ?, DEUnit_ReservedValue4 = ?, 
                         DEUnit_ReservedValue7 = ?, DEUnit_ReservedValue8 = ?, DEUnit_ReservedValue9 = ?, DEUnit_ReservedValue10 = ?, DEUnit_ReservedValue11 = ?, DEUnit_ReservedValue12 = ?, DEUnit_ReservedValue13 = ?,
                          DEUnit_ReservedValue14 = ?, DEUnit_ReservedValue15 = ?, DEUnit_ReservedValue22 = ?, DEUnit_ReservedValue27 = ?
WHERE        (config_ID = ?)

只有在使用 PREPARE 和 EXECUTE 语法时才能使用 ? 占位符。阅读 https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html 了解详情。

但即使您确实使用了 PREPARE & EXECUTE,您仍然需要在将变量传递给 EXECUTE 时为其命名。

在存储过程中,您可以使用变量而不是占位符。

CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)
BEGIN
  SELECT COUNT(*) INTO cities FROM world.city
  WHERE CountryCode = country;
END

在该示例中,country 是过程输入参数。您可以直接在 SQL 语句中使用它(在本例中为 SELECT,但它也适用于 UPDATE)。

确保您的参数名称与您查询的 table 中的任何列都不相同,因为这会产生歧义。