如果要更新的列的值与试图更新的列的值不同,我想停止更新查询。我有这个触发器:
create trigger dbo.NoUpdate
on HistoricalValues With Encryption
for update
as
if update(value)
BEGIN
rollback transaction
END
但是我想添加这样的东西(伪代码):
if update(value) AND oldValue != newValue
rollback transaction
其中oldValue是当前行中的值,新值是update语句中的建议值。
这至少必须与Sql Server Express 2014一起使用。
为了扩展我的评论“如果您不希望人们UPDATE
特定列,使用基于权限的方法会更好吗?”,我个人会这样做:
CREATE TABLE dbo.YourTable (id int IDENTITY,
GoodColumn varchar(10),
BadColumn varchar(10));
GO
CREATE USER TestUser WITHOUT LOGIN;
GO
GRANT SELECT, UPDATE, DELETE, INSERT ON dbo.YourTable TO TestUser;
GO
DENY UPDATE ON dbo.YourTable(BadColumn) TO TestUser;
GO
INSERT INTO dbo.YourTable (GoodColumn,
BadColumn)
VALUES('test123','example12'),
('test456','example34');
GO
EXECUTE AS USER = 'TestUser';
GO
UPDATE dbo.YourTable
SET GoodColumn = 'Test789'
WHERE BadColumn = 'example34';
GO
UPDATE dbo.YourTable
SET BadColumn = 'Example56'
WHERE GoodColumn = 'test123';
GO
REVERT;
GO
SELECT *
FROM dbo.YourTable;
GO
DROP TABLE dbo.YourTable;
GO
DROP USER TestUser;