[英]Trying to resolve a syntax error for MYSQL update trigger
I am trying to create a trigger to update the Project
table whenever the Assign
table is updated, yet I keep receiving this error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF
我正在尝试创建一个触发器以在更新Assign
表时更新Project
表,但我一直收到此错误: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF
Does anyone have any idea of why I am getting this syntax error?有没有人知道为什么我会收到这个语法错误? Please note that I am a beginner at this so my SQL statements may be entirely wrong.请注意,我是初学者,所以我的 SQL 语句可能完全错误。 Also, I know for sure that my query being ran in my IF statement works because I've ran it on its own without being inside a trigger.此外,我确信我的查询在我的 IF 语句中运行是有效的,因为我在没有在触发器内的情况下自行运行了它。
delimiter $$
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
#RUN THIS QUERY TO UPDATE MY TABLE
UPDATE Project p JOIN
(SELECT Project.projNo, COUNT(DISTINCT empid) as numEmployeeAssigned
FROM (Project LEFT JOIN Assign ON Project.projNo=Assign.projNo)
GROUP BY projNo
) as tb
ON p.projNo = tb.projNo
SET p.numEmployeeAssigned = tb.numEmployeeAssigned
#END QUERY
END IF
END$$
delimiter;
You can just increment or decrement the count:您可以增加或减少计数:
UPDATE Project p
SET p.numEmployeeAssigned = p.numEmployeeAssigned +
(case when p.projNo = new.ProjNo then 1 else -1 end)
WHERE p.projNo IN (old.ProjNo, new.ProjNo) AND
old.ProjNo <> new.ProjNo;
Obviously, your code is missing semi-colons after the UPDATE
statement and after the END IF
.显然,您的代码在UPDATE
语句之后和END IF
之后缺少分号。
However, let me suggest that the UPDATE
statement could most likely be simplified.但是,让我建议最有可能简化UPDATE
语句。 As I understand your code, you want to update the count of employees in Project
whenever an employee changes its assignment.据我了解您的代码,您希望在员工更改其分配时更新Project
的员工人数。 If so, there is no need to actually query assign
.如果是这样,则无需实际查询assign
。 You can just use conditional logic like this:您可以像这样使用条件逻辑:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo);
END IF;
END$$
You could even remove the IF
statement and put that condition in the query directly:您甚至可以删除IF
语句并将该条件直接放入查询中:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo) AND OLD.projNo <> NEW.projNo
END$$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.