Mysql Trigger error : SQLSTATE[42000]: Syntax error or access violation: 1231 Variable 'MESSAGE_TEXT' can't be set to the value of 'NULL'

Viewed 152

I am trying to add some triggers to my database and getting this error : SQLSTATE[42000]: Syntax error or access violation: 1231 Variable 'MESSAGE_TEXT' can't be set to the value of 'NULL'

MySQL version : 8.0.26

Here is a before update trigger example:

CREATE 
DEFINER = 'admin'@'%'
TRIGGER db2.members_b_u
    BEFORE UPDATE
    ON db2.mymembers
    FOR EACH ROW
begin
    declare msg VARCHAR(200);
    DECLARE userid1 INT;
    if new.memberid IS null then
        set msg = CONCAT('Error .: ', cast(new.userid as char));
        SIGNAL sqlstate '45000' set message_text = msg;
   end if;
    if new.memberlevel <>'Π19' AND new.memberlevel <>'ΜΜ' AND new.memberlevel is not null AND new.city IS NULL then
        set msg = CONCAT('Error : ', cast(new.am as char));
        SIGNAL sqlstate '45000' set message_text = msg;
   end if;
    if LENGTH(new.postalcode1)<>5 AND new.city1 between '01' and '99' then
        set msg = CONCAT('Error : ', cast(new.userid as char));
        SIGNAL sqlstate '45000' set message_text = msg;
   end if;
   select userid into userid1 from mymembers where memberid=new.memberid AND userid!=NEW.userid;
        set msg = CONCAT('Error : ', cast(userid1 as char));
        SIGNAL sqlstate '45000' set message_text = msg;
END

Any help appreciate it !

1 Answers

concat returns null if any of its arguments are null, which could very well happen here. One way to safeguard against it would be to use coalesce. E.g.:

set msg = CONCAT('Error .: ', COALESCE(cast(new.userid as char), 'Missing userid');
Related