Reputation: 63
I'm working on an 'audit_history' table that stores updates performed on my 'myTable' table. My code works with just the single IF statement that handles password updates. But as soon as I add another, I get the #1064 SQL Error. So I'm guessing it's something to do with my delimiters(?), but I just can't get it to work.
Any and all help would be great. Thanks.
IF (NEW.password <> OLD.password)
OR (NEW.password IS NOT NULL AND OLD.password IS NULL)
OR (NEW.password IS NULL AND OLD.password IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, @user_id
, 'user'
, 'password'
, OLD.password
, NEW.password);
END IF;
IF (NEW.landline <> OLD.landline)
OR (NEW.landline IS NOT NULL AND OLD.landline IS NULL)
OR (NEW.landline IS NULL AND OLD.landline IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, @user_id
, 'user'
, 'landline'
, OLD.landline
, NEW.landline);
END IF;
I get this error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to you MySQL server version for the right syntax to use near ‘IF (NEW.landline <> OLD.landline) OR (NEW.landline IS NOT NULL AND OLD.landline’ at line 17
Upvotes: 2
Views: 2973
Reputation: 41
You need BEGIN and END keywords for multiple statements
BEGIN
IF (NEW.password <> OLD.password)
OR (NEW.password IS NOT NULL AND OLD.password IS NULL)
OR (NEW.password IS NULL AND OLD.password IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, @user_id
, 'user'
, 'password'
, OLD.password
, NEW.password);
END IF;
IF (NEW.landline <> OLD.landline)
OR (NEW.landline IS NOT NULL AND OLD.landline IS NULL)
OR (NEW.landline IS NULL AND OLD.landline IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, @user_id
, 'user'
, 'landline'
, OLD.landline
, NEW.landline);
END IF;
END;
Upvotes: 4