1

I am trying to figure out how to throw an error with formatted error message in 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

How can I use "something" variable when creating error message?

1

2 Answers 2

2
// 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

Following @Samis comment and retrying using CONCAT - above works.

Sign up to request clarification or add additional context in comments.

Comments

1

What about CONCAT() and local variable to store the message?

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

working example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.