This is probably a stupid mistake but I can't figure out what causes the error. I'm trying to compare the sum of Employee cost in one plan to the budget of the project before each insert. This is for sqlite, thanks for any answers in advance.
%%sql
CREATE TRIGGER trg_ins_PlanEmployees BEFORE INSERT ON PlanEmployees
BEGIN
SELECT
CASE
WHEN NOT EXISTS (
SELECT 1
FROM Project pro
INNER JOIN Plan ON pro.projectID = Plan.projectID
INNER JOIN PlanEmployees ON Plan.pID = PlanEmployees.pID
INNER JOIN Employee ON PlanEmployee.eID = Employee.eID
WHERE SUM(cost) FROM Employee <= project.budget
)
THEN RAISE (ABORT, 'Over budget')
END;
END;
CodePudding user response:
The query of
SELECT 1
FROM Project pro
INNER JOIN Plan ON pro.projectID = Plan.projectID
INNER JOIN PlanEmployees ON Plan.pID = PlanEmployees.pID
INNER JOIN Employee ON PlanEmployee.eID = Employee.eID
WHERE SUM(cost) FROM Employee <= project.budget
is syntactically incorrect because of the
WHERE SUM(cost) FROM Employee <= project.budget
part, because SUM
is an aggregate function that does not evaluate to boolean and is incorrect by itself to form the where clause and the FROM
cannot be defined twice, nor after the WHERE
.
Possible fix:
SELECT 1
FROM Project pro
INNER JOIN Plan ON pro.projectID = Plan.projectID
INNER JOIN PlanEmployees ON Plan.pID = PlanEmployees.pID
INNER JOIN Employee ON PlanEmployee.eID = Employee.eID
WHERE SUM(cost) <= project.budget