第一次在这里发帖。
我想创建一个触发器,以便当有人尝试更新表时,它会阻止更新并将尝试记录在审计表中。
USE [AdventureWorks2008R2]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [Person].[Lastname_Trigger]
ON [Person].[Person]
FOR UPDATE AS
IF UPDATE (Lastname)
BEGIN
INSERT INTO SurnameChange (BusinessEntityID, Firstname, OldLastName, NewLastName,AttemptedBy, Timestamped)
SELECT
d.businessentityid,
i.firstname,
d.lastname,
i.lastname,
SUSER_SNAME(),
GETDATE()
FROM deleted d , inserted i
WHERE d.businessentityid = i.businessentityid
END
/*This is where it is going wrong*/
BEGIN TRANSACTION
IF UPDATE(lastname)
BEGIN
RAISERROR ('cannot change lastname', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
如果有人可以帮助我,那就太好了,我已经使用 AdventureWorks 作为示例,因此它对其他人来说是通用的和可用的。
非常感谢。
詹姆士