Throw an error in a MySQL trigger

后端 未结 7 1195
-上瘾入骨i
-上瘾入骨i 2020-11-22 11:59

If I have a trigger before the update on a table, how can I throw an error that prevents the update on that table?

7条回答
  •  暖寄归人
    2020-11-22 12:52

    The following procedure is (on mysql5) a way to throw custom errors , and log them at the same time:

    create table mysql_error_generator(error_field varchar(64) unique) engine INNODB;
    DELIMITER $$
    CREATE PROCEDURE throwCustomError(IN errorText VARCHAR(44))
    BEGIN
        DECLARE errorWithDate varchar(64);
        select concat("[",DATE_FORMAT(now(),"%Y%m%d %T"),"] ", errorText) into errorWithDate;
        INSERT IGNORE INTO mysql_error_generator(error_field) VALUES (errorWithDate);
        INSERT INTO mysql_error_generator(error_field) VALUES (errorWithDate);
    END;
    $$
    DELIMITER ;
    
    
    call throwCustomError("Custom error message with log support.");
    

提交回复
热议问题