简体   繁体   English

如何在MySQL中准备和执行多个语句?

[英]How to prepare and execute multiple statements in MySQL?

I have a stored procedure on MySQL db that I use to update my records. 我在MySQL数据库上有一个存储过程,用于更新记录。 Based on the inputs, either, add hours to a column, or subtract hours to a column. 根据输入,或者将小时添加到列中,或者减去小时到列中。 I call this SP using the Appian smart service for store procedures. 我使用Appian智能服务进行存储过程调用此SP。 My problem is, that if tried to do both, add and subtract (for different rows) then the store procedure doesn't do anything. 我的问题是,如果尝试同时进行加和减(对于不同的行),则存储过程将不执行任何操作。 It runs in my process model fine but the DB is not updated. 它可以在我的流程模型中正常运行,但数据库未更新。 If I run the PM with a only one input(either add or subtract) then it updates the db accordingly. 如果我仅使用一个输入(加法或减法)运行PM,则它将相应地更新数据库。 Could anyone tell me what I'm doing wrong in my SP? 谁能告诉我我在SP中做错了什么? Note: addAttendeeUsername and removeAttendeeUsername inputs are both VARCHAR's strings delimited by commas 注意: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

It's not clear because you haven't provided the procedure argument declaration, but I would guess addAttendeeUsername and removeAttendeeUsername are strings. 尚不清楚,因为您尚未提供过程参数声明,但是我猜addAttendeeUsernameremoveAttendeeUsername是字符串。 But you don't use quotes around them in your SQL syntax. 但是,您不要在SQL语法中使用引号。 So they'll be interpreted as column names, which are no doubt not found. 因此,它们将被解释为列名,这无疑是找不到的。

You really should be combining input variables using query parameters, NOT concatenating them into your 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;

When you EXECUTE , you must use session variables (the kind with @ sigil), not local variables. EXECUTE ,必须使用会话变量(带有@ sigil的变量),而不是局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM