如何为SQL Server编写这个触发器?
假设我有一个表 my_table(id, x, y)
。我想编写一个触发器来防止更新 y
col 并将其设置为 non-null
值(如果 x
已为 null)
。由于 SQL Server 没有更新前触发器,那么如何做到这一点呢?显然我们可以使用而不是
触发器来达到此目的,但是我们如何检查旧值和当前值并决定是否应该引发错误或让更新正常执行?
示例:
假设我们在数据库中有这一行:
1, null, null
那么这应该会失败(引发错误)
update my_table set y = 'blah' where id = 1;
但这应该会成功:
update my_table set y = null where id = 1;
我知道这个示例不是很有意义,但它与我想要实现的目标类似。
Suppose that I have a table my_table(id, x, y)
. I want to write a trigger to prevent updating the y
col and setting it to a non-null
value if x
is already null
. As SQL Server doesn't have a before update trigger, how can this be done? Apparently we can use an instead of
trigger for this purpose, but how can we check the old and current values and decide whether we should raise an error or let the update execute normally?
Example:
Let's pretend we have this row in the DB:
1, null, null
Then this should fail (raise error)
update my_table set y = 'blah' where id = 1;
But this should succeed:
update my_table set y = null where id = 1;
I know the example isn't very meaningful, but it is similar to what I am trying to achieve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可行,但我不确定您需要处理哪些其他边缘条件:
This should work, but I am not sure what other edge conditions you need to handle: