我的SQL Server数据库中有两个表Trainer(TrainerID, Username, Password, email, .... )
和TrainerPassword (ID, TrainerID, Password, CreationDate)
。 TrainerPassword(TrainerID)指Trainer(TrainerID)。任务的基线是跟踪培训师的密码更改。我在Trainer表上使用了Trigger。
现在问题出现了如何触发事件的触发器
这两个事件都可以在单个触发器中实现吗?
答案 0 :(得分:2)
我会做这样的事......
CREATE TRIGGER tr_Trainer_For_Insert_Update
ON Trainer
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO TrainerPassword (TrainerID, [Password], CreationDate)
SELECT TrainerID, [Password] , GETDATE()
FROM inserted
END
CREATE TRIGGER tr_Trainer_For_Delete
ON Trainer
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM TrainerPassword
WHERE EXISTS (SELECT 1 FROM deleted
WHERE TrainerID = TrainerPassword.TrainerID )
END