我正在尝试创建一个触发器,以使用人员名字的前两个字符和姓氏的前三个字符生成唯一 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 @@