Answered by:

Number of employees in a department of a branch

Question

Hi I am having a few problems trying to complete this project and need some help.

I need to create an after trigger that displays/prints the number of employees in a department and branch after a row is inserted and then returns the counted number
of employees to the department and branch table. Thanks

Answers

create trigger trEmployeesInsert on Employee AFTER INSERT AS
BEGIN
;merge dept as Target
using (select count(*) as cntNew, dpNo FROM Inserted
GROUP BY dpNo) as Source on Target.dpNo = Source.dpNo
WHEN Matched

THEN UPDATE SET NonEmployee = NonEmployee + Source.cntNew;
;merge Branch as Target
using (select count(*) as cntNew, BrNo FROM Inserted
GROUP BY BrNo) as Source on Target.BrNo = Source.BrNo
WHEN Matched

THEN UPDATE SET NoOfEmployee = NoOfEmployee + Source.cntNew;

For every expert, there is an equal and opposite expert. - Becker's Law

create trigger trEmployeesInsert on Employee AFTER INSERT AS
BEGIN
;merge dept as Target
using (select count(*) as cntNew, dpNo FROM Inserted
GROUP BY dpNo) as Source on Target.dpNo = Source.dpNo
WHEN Matched

THEN UPDATE SET NonEmployee = NonEmployee + Source.cntNew;
;merge Branch as Target
using (select count(*) as cntNew, BrNo FROM Inserted
GROUP BY BrNo) as Source on Target.BrNo = Source.BrNo
WHEN Matched

THEN UPDATE SET NoOfEmployee = NoOfEmployee + Source.cntNew;

For every expert, there is an equal and opposite expert. - Becker's Law

Hi I am having a few problems trying to complete this project and need some help.

I need to create an after trigger that displays/prints the number of employees in a department and branch after a row is inserted and then returns the counted number of employees
to the department and branch table. Thanks