MsSQL DB 上 INSTEAD OF UPDATE 触发器的默认操作
我在 MsSQL 中遇到 INSTEAD OF 触发器问题。我的应用程序出现一些错误,作为一种快速解决方法,我不希望用户修改数据库中的某一行。 我创建了以下触发器:(在桌子上)
create trigger UglyWorkaround ON Configuration instead of update
as
begin
if (select COUNT(1) from inserted where Key='Key01' and Value<>'2') > 0 begin
update Configuration set Value='2' where Key='Key01'
end else begin
-- DEFAULT ACTION (do update as intended)
end;
end;
但是我在确定如何设置默认操作时遇到问题。 更新配置集 Value=inserted.Value where Key=inserted.Key
对我不起作用。有什么办法可以用触发器来做到这一点吗? (我知道这个解决方案很糟糕,但我没有其他选择,因为我现在无法更改代码。)
I've got problem with INSTEAD OF trigger in MsSQL. I got an app with some error, and as a quick workaround I don't want user to modify one exact row in DB.
I created following trigger: (on table)
create trigger UglyWorkaround ON Configuration instead of update
as
begin
if (select COUNT(1) from inserted where Key='Key01' and Value<>'2') > 0 begin
update Configuration set Value='2' where Key='Key01'
end else begin
-- DEFAULT ACTION (do update as intended)
end;
end;
But I've got problem with determining, how to set default action.Update Configuration set Value=inserted.Value where Key=inserted.Key
doesn't work for me. Is there any way how to do this with triggers? (I know that the solution is bad, but I got no other option, as I can't change code now.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
inserted
是一个表,因此请尝试加入:您也可以同时过滤掉
Key01
,如果他们尝试更新的值也没关系>Key01
为2
之外的其他内容。inserted
is a table, so try joining:You could also filter out
Key01
at the same time, and it wouldn't matter if they tried to update the value forKey01
to something other than2
.INSTEAD OF 部分意味着您将必须更新其他列以及值列。
无需使用 if .. else 执行此操作,因为逻辑可以在列级别完成
The INSTEAD OF part means that you're going to have to update the other columns as well as the value one.
There's no need to do this with an if .. else as the logic can be done at column level
如果您想阻止编辑对更改执行
回滚
,为什么在找到匹配行时设置Value='2'
?您不必提供默认操作,在触发器中不执行任何操作都会让更新发生。
If you want to prevent the edit perform a
rollback
on the change, why are you settingValue='2'
when you find a matching row?You don't have to supply a default action, doing nothing in the trigger will let the update happen.