(PLSQL) 在 Oracle 更新触发器中测试更改值的最简单表达式是什么?
这是一个可以解决问题的布尔表达式:
nvl(:new.location != :old.location, (:new.location is null) != (:old.location is null))
但我想有一个更简单的表达式。有什么想法吗?
Here is a boolean expression that does the trick:
nvl(:new.location != :old.location, (:new.location is null) != (:old.location is null))
But I would like to think there was a simpler expression. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建一个像这样的重载包函数:
然后只需在触发器中调用它:
或者您也可以这样做:
当然,您必须选择一个永远可能是真实位置的值,该值在某些情况下可能会很棘手。对于 VARCHAR2,您可以选择一个对于列大小来说太长的值(除非恰好是 4000)。
You could create an overloaded package function like this:
Then just call it in your triggers:
Alternatively you can just do this:
Of course, you have to pick a value that could never be a real location, which may be tricky in some cases. For VARCHAR2 you could pick a value that's too long for the column size (unless that happens to be 4000).
编辑:这样说可能更清楚:
edit: It might be more clear to say:
也许您只想在值发生变化时触发触发器?如果是这样,您最好在创建触发器时使用以下语法:
Perhaps you only want the trigger to fire at all if the value has changed? If so, you are best to use this syntax when creating the trigger:
这些较短的方法都有几个缺点。
它们很慢,不直观,可能有错误(尽可能避免魔法值),
并且比 AND/OR/IS NULL/IS NOT NULL 等正常条件更具专有性。
NVL、DECODE、COALESCE 等,可能比您想象的更昂贵。
我已经在几个不同的上下文中多次看到过这种情况,这里有一个简单的例子:
我建议您多花一点时间以合乎逻辑的方式输入它。您的代码将会看起来更好并且运行得更快。
These shorter methods all have several disadvantages.
They are slow, unintuitive, potentially buggy (avoid magic values whenever possible),
and more proprietary than normal conditions like AND/OR/IS NULL/IS NOT NULL.
NVL, DECODE, COALESCE, etc., can be more expensive than you think.
I've seen this lots of times in several different contexts, here's a simple example:
I recommend you spend the extra second to type it the logical way. Your code will look better and run faster.