如果我输入一个不同的值,它将起作用。它返回了消息文本“该电子书不存在”的值

如果我将确切的值放在数据库中,则它只会执行,而不会返回任何值。为什么?

CREATE PROCEDURE SPBus (
        IN BUS VARCHAR (20)
    )
    BEGIN

    START TRANSACTION;
    IF  NOT EXISTS (SELECT * FROM Ebook WHERE TituloEbook LIKE CONCAT('%',BUS,'%' )) THEN
       SIGNAL SQLSTATE '45000'
       SET MESSAGE_TEXT= 'That ebook doesnt exists';
    ELSE
        SELECT * FROM Ebook WHERE TituloEbook LIKE CONCAT('%',BUS, '%');
    END IF;
    COMMIT;
END;

最佳答案

因为这样,将在不设置MESSAGE_TEXT的地方执行该过程的其他情况。试试这个:

CREATE PROCEDURE SPBus (
        IN BUS VARCHAR (20)
    )
    BEGIN

    START TRANSACTION;
    IF  NOT EXISTS (SELECT * FROM Ebook WHERE TituloEbook LIKE CONCAT('%',BUS,'%' )) THEN
       SIGNAL SQLSTATE '45000'
       SET MESSAGE_TEXT= 'That ebook doesnt exists';
    ELSE
        SELECT * FROM Ebook WHERE TituloEbook LIKE CONCAT('%',BUS, '%');
        SET MESSAGE_TEXT = 'That ebook exists';
    END IF;
    COMMIT;
END;

10-07 15:44