0
CREATE TRIGGER TR_Update_Member
AFTER UPDATE ON `member` 
FOR EACH ROW
BEGIN
DECLARE changeNote VARCHAR(5000) DEFAULT '';

SET changeNote = IF(OLD.Name != NEW.Name, 
                    CONCAT( changeNote, 
                            'Name(', 
                            IFNULL(OLD.Name, '--'), 
                            '->', 
                            IFNULL(NEW.Name, '--'), 
                            '), '
                          ), 
                    changeNote);

SELECT TRIM(TRAILING ', ' FROM changeNote) INTO changeNote;

INSERT INTO `member_change_log`(`Name`) VALUES(changeNote)

END

The above trigger does not insert any data when the name contains null. Could anyone please what is wrong with my code.

1 Answer 1

1

The above trigger does not insert any data when the name contains null. Could anyone please what is wrong with my code.

If NEW.Name is NULL then OLD.Name != NEW.Name is NULL too, and IF() executes alternative variant, i.e. you obtain

SET changeNote = changeNote

Simply swap variants:

SET changeNote = IF( OLD.Name = NEW.Name, changeNote, CONCAT( ... ) );

If both OLD.Name and NEW.Name may be NULL then use null-safe compare operator <=> instead of regular compare =.

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

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.