为什么会发生这种情况?

时间:2019-03-05 10:14:39

标签: mysql

create trigger after_user_register 
before INSERT on Users 
for EACH ROW 
begin 
declare @id integer;
declare @userName varchar(30);
declare @userSurname varchar(30);
select
  @id = userId,
  @userName = userName,
  @userSurname = userSurname
from
  inserted;
insert into
  UsersFullName
values(DEFAULT, @id, concat(@userName, @userSurname)) end

我的第一个声明出现#1064错误。您能帮我消除此错误吗?

1 个答案:

答案 0 :(得分:1)

您的CREATE TRIGGER遇到了一些问题。您可以使用以下更简单的解决方案:

DELIMITER //

CREATE TRIGGER after_user_register 
AFTER INSERT on Users 
FOR EACH ROW 
  BEGIN 
    INSERT INTO UsersFullName (id, fullname) 
      SELECT new.userId, CONCAT(new.userName, new.userSurname); 
  END 
//

DELIMITER ;
  

演示: https://www.db-fiddle.com/f/bE5DN43hjX7TtSpS3GDNRn/0