'Correct syntax to use for an If condition in trigger in MariaDB server version
CREATE TRIGGER insertCompanyUser AFTER INSERT ON userinfo
FOR EACH ROW
BEGIN
IF New.companyId is not null THEN
INSERT INTO `tb_companyuser` (`id`, `companyId`, `userId`, `freeze`, `role`, `createDate`, `updateDate`) VALUES (NULL, New.companyId, New.id, b'00000', '1', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
END IF
END
error==> 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF END' at line 6
Solution 1:[1]
I had the very same problem you had, I tried everything and it finally worked when I put ";" after my WHERE statement
CREATE DEFINER=`root`@`localhost` TRIGGER `updater` AFTER UPDATE ON `tbl_transport`
FOR EACH ROW
IF (NEW.`customer_id` = '0')
THEN UPDATE tbl_user SET tbl_user.`user_requested_car` = false
WHERE tbl_user.`user_email` = NEW.`customer_id`;
END IF;
Solution 2:[2]
You should add an ";" after the "END IF".
Solution 3:[3]
DELIMITER //
CREATE TRIGGER insertCompanyUser AFTER INSERT ON userinfo
FOR EACH ROW
BEGIN
IF New.companyId is not null THEN
INSERT INTO `tb_companyuser` (`id`, `companyId`, `userId`, `freeze`, `role`, `createDate`, `updateDate`) VALUES (NULL, New.companyId, New.id, b'00000', '1', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
END IF;
END; //
DELIMITER ;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Christoforos |
| Solution 3 | Mikey |

