我在MySQL数据库上有一个存储过程,用于更新我的记录。根据输入,向列添加小时数,或向列减去小时数。我使用 Appian 智能服务来调用此 SP,用于存储程序。我的问题是,如果尝试同时执行这两项操作,则加减(针对不同的行(,则存储过程不会执行任何操作。它在我的进程模型中运行良好,但数据库未更新。如果我只用一个输入(加或减(运行 PM,那么它会相应地更新数据库。谁能告诉我我在SP中做错了什么?注意:addAttendeeUsername 和 removeAttendeeUsername 输入都是 VARCHAR 的字符串,用逗号分隔
BEGIN
DECLARE startTime datetime;
DECLARE SQLStateCode varchar(5) DEFAULT '00000';
DECLARE ErrorNumber int;
DECLARE MessageText varchar(1000);
-- insert / update
set @sql = concat("UPDATE
XCS_APP_CERTIFICATION
SET
COMPLETED_HOURS = COMPLETED_HOURS + ", courseHours , ",
LAST_MODIFIED_BY = '", lastModifiedBy, "',
LAST_MODIFIED_ON = NOW()
WHERE
XCS_APP_CERTIFICATION.CREATED_BY in (" , addAttendeeUsername , ") AND VALID_START_DATE <= '", offeringEndDateTime, "'
AND GRACE_PER_END_DATE >= '", offeringEndDateTime,"' ");
set @sql2 = concat("UPDATE
XCS_APP_CERTIFICATION
SET
COMPLETED_HOURS = COMPLETED_HOURS - ", courseHours, ",
LAST_MODIFIED_BY = '", lastModifiedBy, "',
LAST_MODIFIED_ON = NOW()
WHERE
CREATED_BY IN (", removeAttendeeUsername, ") AND VALID_START_DATE <= '", offeringEndDateTime, "'
AND GRACE_PER_END_DATE >= '", offeringEndDateTime, "' ");
PREPARE stmt FROM @sql;
EXECUTE stmt;
PREPARE stmt2 FROM @sql2;
EXECUTE stmt2;
set successFlag = 1;
-- committing transaction
COMMIT;
END
因为您没有提供过程参数声明,但我猜addAttendeeUsername
和removeAttendeeUsername
是字符串。但是,不要在 SQL 语法中使用引号。因此,它们将被解释为列名
您确实应该使用查询参数组合输入变量,而不是将它们连接到SQL中。
set @sql = concat("UPDATE
XCS_APP_CERTIFICATION
SET
COMPLETED_HOURS = COMPLETED_HOURS + ?
LAST_MODIFIED_BY = ?,
LAST_MODIFIED_ON = NOW()
WHERE
XCS_APP_CERTIFICATION.CREATED_BY in (?) AND VALID_START_DATE <= ?
AND GRACE_PER_END_DATE >= ? ");
SET @courseHours = courseHours,
@lastModifiedBy = lastModifiedBy,
@addAttendeeUsername = addAttendeeUsername,
@offeringEndDateTime = offeringEndDateTime;
PREPARE stmt FROM @sql;
EXECUTE stmt USING @courseHours, @lastModifiedBy,@addAttendeeUsername,
@offeringEndDateTime, @offeringEndDateTime;
EXECUTE
时,必须使用会话变量(带有@
符号的那种(,而不是局部变量。