对于我的生活,我看不出这个触发器的语法有什么问题。 MySQLworkbench说我在第一个SET命令结束时错过了一个分号,但添加了另一个没有帮助。这是sql:
create trigger insert_user_trigger
before insert
on ds_users
for each row
BEGIN
IF (char_length(new.FirstName) + char_length(new.LastName)) <= 20 THEN
set new.Username = concat(new.FirstName,'.',new.LastName);
ELSE
set new.Username = concat(LEFT(new.FirstName,1),'.', LEFT(new.LastName,18));
ENDIF;
END;
我想要完成的是从名字和姓氏列构建用户名列值。但是,“First.Last”组合不能超过20个字符。所以我想继续使用名字的第一个字符“the”。如果总数超过20个字符,则最多可使用18个姓氏字符。
通过mysql命令行界面运行create的输出:
mysql> create trigger insert_user_trigger
-> before insert
-> on ds_users for each row
-> BEGIN
-> IF (char_length(new.FirstName) + char_length(new.LastName)) <= 20 THEN SET new.Username = concat(new.FirstName,'.',new.LastName);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
mysql> ELSE SET new.Username = concat(LEFT(new.FirstName,1),'.', LEFT(new.LastName,18));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ELSE SET new.Username = concat(LEFT(new.FirstName,1),'.', LEFT(new.LastName,18)' at line 1
mysql> ENDIF;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ENDIF' at line 1
mysql> END;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1