0

我正在尝试创建一个触发器,以使用人员名字的前两个字符和姓氏的前三个字符生成唯一 ID。这是我目前正在解决的错误:

#1064 - 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 '@firstID varchar(2), DECLARE @lastID varchar (3), DECLARE @newID varchar (5); ' at line 3

我也不认为我可以只使用“+”来添加两个(字符串)变量,但CONCAT也不起作用。请帮忙!

DELIMITER @@
CREATE TRIGGER EID BEFORE INSERT ON edrstaff FOR EACH ROW
BEGIN
DECLARE @firstID varchar(2),
DECLARE @lastID varchar (3),
DECLARE @newID varchar (5);

SET @firstID = (
SELECT SUBSTRING(fname, FROM 1 FOR 2))
FROM edrstaff
WHERE fname = new.fname)

SET @lastID = (SELECT SUBSTRING(lname, FROM 1 FOR 3))

FROM edrstaff
WHERE lname = new.lname); 

SET @newID = @firstID + @lastID; 

END @@
4

1 回答 1

0

尝试

CREATE TRIGGER EID 
BEFORE INSERT ON edrstaff 
FOR EACH ROW
    SET NEW.id = CONCAT(LEFT(NEW.fname, 2), LEFT(NEW.lname, 3));

SQLFiddle

于 2013-05-12T07:51:46.180 回答