DELIMITER $$
CREATE TRIGGER `tr_tblEmployee_Update` BEFORE UPDATE ON `tblEmployee` FOR EACH ROW
BEGIN
DECLARE id int; DECLARE OldGender varchar(10); DECLARE NewGender varchar(10); DECLARE NewName varchar(50); DECLARE OldName varchar(50);
DECLARE OldSalary int; DECLARE NewSalary int; DECLARE OldID_Dep int; DECLARE NewId_Dep int;
DECLARE AuditString varchar(300);
SET id = ID_Employee; SET NewGender = Gender; SET NewName = Name; SET NewSalary = Salary; SET NewID_Dep = ID_Dep;
SET OldGender = Gender; SET OldName = Name; SET OldSalary = Salary; SET OldID_Dep = ID_Dep;
SET AuditString = concat('Employee with id = ',id,' changed: ')
IF(OldName<>NewName)
SET AuditString = concat(AuditString, 'Name from ',OldName,' to ',NewName);
IF(OldGender<>NewGender)
SET AuditString = concat(AuditString, ' Gender from ',OldGender,' to ',NewGender);
IF(OldSalary<>NewSalary)
SET AuditString = concat(AuditString, ' Salary from ',OldName,' to ',NewName);
IF(OldDep<>NewDep)
SET AuditString = concat(AuditString, ' Department from ',OldName,' to ',NewName);
INSERT INTO tblemployeeaudit(AuditData) VALUES(AuditString);
END
$$
DELIMITER ;
您好,我想多次使用具有相同变量(AuditString)的concat函数,但每次尝试执行此操作时都会出错。
错误 - #1064 - 您的SQL语法出错;检查手册 对应于您的MariaDB服务器版本以获得正确的语法 在'IF(OldName!= NewName)附近使用 SET AuditString = concat(AuditString,'Name from',Ol'... at line 12
答案 0 :(得分:0)
这是最终解决方案,以防其他人遇到与我相同的问题。
DELIMITER $$
在tr_tblEmployee_Update
之前为每个行创建TRIGGER tblEmployee
BEGIN
DECLARE id int; DECLARE OldGender varchar(10); DECLARE NewGender varchar(10); DECLARE NewName varchar(50); DECLARE OldName varchar(50); DECLARE OldSalary int; DECLARE NewSalary int; DECLARE OldID_Dep int; DECLARE NewId_Dep int; DECLARE AuditString varchar(300);
SET id = OLD.ID_Employee; SET NewGender = NEW.Gender; SET NewName = NEW.NAME; SET NewSalary = NEW.Salary;
SET OldGender = OLD.Gender; SET OldName = OLD.NAME; SET OldSalary = OLD.Salary;
SET AuditString = concat('Employee with id = ',id,' changed: ');
IF(OldName<>NewName) THEN
SET AuditString = concat(AuditString, 'Name from ',OldName,' to ',NewName);
END IF;
IF(OldGender<>NewGender) THEN
SET AuditString = concat(AuditString, ' Gender from ',OldGender,' to ',NewGender);
END IF;
IF(OldSalary<>NewSalary) THEN
SET AuditString = concat(AuditString, ' Salary from ',OldSalary,' to ',NewSalary);
END IF;
INSERT INTO tblemployeeaudit(AuditData) VALUES(AuditString);
END