如何在过程中连接更新查询

wvmv3b1j  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(333)

我正在尝试创建一个过程,以便在修复错误时更新行。下面是它的样子:

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 价值?

cgvd09ve

cgvd09ve1#

为什么要使用动态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 如果是整数,则此操作将始终失败。

相关问题