如何在过程中配置更新查询

时间:2018-11-17 13:49:25

标签: mysql sql stored-procedures

我正在尝试创建一个程序,以在修复错误后更新行。看起来像这样:

DELIMITER $$
CREATE PROCEDURE bugFix (
    IN rid_in INT,
    IN fix_details_in VARCHAR (300)
) BEGIN
    SET @resolved_qry = CONCAT('UPDATE _bug_report SET resolved = 1 WHERE rid = ', rid_in);
    PREPARE stmt FROM @resolved_qry;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    UPDATE _bug_report SET fix_details = CONCAT(fix_details, fix_details_in) WHERE rid = CONCAT(rid, rid_in);

END $$
DELIMITER ;

将解析类型更改为1时:

SET @resolved_qry = CONCAT('UPDATE _bug_report SET resolved = 1 WHERE rid = ', rid_in);
PREPARE stmt FROM @resolved_qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

它工作正常,但是在更新fix_details列以包括其详细信息时,它不起作用:

UPDATE _bug_report SET fix_details = CONCAT(fix_details, fix_details_in) WHERE rid = CONCAT(rid, rid_in);

这就是我的称呼:

CALL bugFix(1, 'Hey, we fixed it');

有任何想法如何使用fix_details_in值吗?

1 个答案:

答案 0 :(得分:1)

为什么要使用动态SQL?

DELIMITER $$
CREATE PROCEDURE bugFix (
    IN rid_in INT,
    IN fix_details_in VARCHAR (300)
) BEGIN
    UPDATE _bug_report
        SET resolved = 1,
            fix_details = CONCAT(fix_details, fix_details_in)
        WHERE rid = rid_in;
END $$
DELIMITER ;

大概,“无效”表示fix_details未被更新。那是因为没有匹配项:

WHERE rid = CONCAT(rid, rid_in)

鉴于rid_in是一个整数,它将始终失败。