在MySQL错误中格式化错误消息

时间:2017-01-28 16:42:48

标签: mysql sql

我试图找出如何在MySQL(5.7)中抛出带有格式化错误消息的错误。

// SOME PROCEDURE
begin
declare something INT;
start transaction;
    call getsomething(something); // sets something to data
    if something is NULL then
        rollback;
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = 'Something %d not found!', MYSQL_ERRNO = 1001;
    end if;
commit;
end

我如何使用"某些东西"创建错误消息时变量?

2 个答案:

答案 0 :(得分:1)

用于存储消息的CONCAT()和局部变量怎么样?

SET MESSAGE_TEXT = CONCAT(something, 'not found!');

working example

答案 1 :(得分:1)

// SOME PROCEDURE
begin
declare something INT;
declare error_msg VARCHAR(255);
start transaction;
    call getsomething(something); // sets something to data
    if something is NULL then
        rollback;
        set error_msg = CONCAT('Something not found!', something);
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = error_msg, MYSQL_ERRNO = 1001;
    end if;
commit;
end

关注@Samis评论并使用CONCAT重试 - 以上工作。