我有2个表Employee
和Employee_Backup
Employee
有3列
和Employee_Backup
也有3列:
对于在表Employee
中插入或更新的每一行,我想使用此条件在表Status
中设置Employee_Backup
WHERE employee_backup.IDEmployee = employee.IDEmployee (inserted / updated)
答案 0 :(得分:0)
那样的东西?
CREATE TRIGGER tr_Employee_Insert
ON dbo.Employee
FOR INSERT
AS
UPDATE b
SET Status = 'Inserted'
FROM dbo.Employee_Backup b
INNER JOIN Inserted i ON b.EmployeeID = i.EmployeeID
CREATE TRIGGER tr_Employee_Update
ON dbo.Employee
FOR UPDATE
AS
UPDATE b
SET Status = 'Updated'
FROM dbo.Employee_Backup b
INNER JOIN Inserted i ON b.EmployeeID = i.EmployeeID
您基本上需要加入 Inserted
伪表,其中包含已从基表(dbo.Employee
)和{{插入(或更新)的所有行1}} table - 然后使用JOIN中的结果集作为Employee_Backup
语句的基础。
注意:当您向UPDATE
添加新行时,不会将任何新行插入Employee_Backup
- 这是您想要的吗?如果没有,您必须稍微更改dbo.Employee
触发器....