为什么不允许MERGE语句的目标表具有已启用的规则? [英] Why is the target table of a MERGE statement not allowed to have enabled rules?

查看:176
本文介绍了为什么不允许MERGE语句的目标表具有已启用的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用以下SQL更新数据库的过程

We have a process updating the database which uses the following SQL

IF NOT EXISTS (SELECT * FROM Target_Table WHERE Target_Table_ID = ...)
BEGIN
    INSERT ...
END
ELSE
BEGIN
    UPDATE ...
END

我想将其更改为使用MERGE语句,以避免可能的竞态条件,在这种情况下,分别评估IFINSERTUPDATE语句.

I wanted to change this to use a MERGE statement in order to avoid a possible race condition where the IF and then INSERT or UPDATE statements are evaluated separately.

但是,一旦执行查询,我就会收到错误消息:

However, as soon as I executed the query I got the error:

MERGE语句的目标表"Target_Table"不能具有任何启用的规则.找到规则"TargetTable_RULE"

The target table 'Target_Table' of the MERGE statement cannot have any enabled rules. Found rule 'TargetTable_RULE'

我了解文档指出,该表无法启用规则,尽管我在初次阅读时就错过了它,除了在执行MERGE之前禁用规则(我认为这不是可行的解决方案)之外似乎我坚持使用IF NOT EXISTS解决方案.

I understand the documentation states that the table cannot have rules enabled, although I missed it when first reading it, and apart from disabling the rule before performing the MERGE (which I don't see as a workable solution) it seems I'm stuck with the IF NOT EXISTS solution.

文档未解释的是为什么不允许目标表具有规则.就我而言,规则是对值进行简单的> 0检查.

What the document does not explain is why the target table is not allowed to have rules. In my case the rule is a simple > 0 check on a value.

有人知道为什么在这种情况下不允许使用规则吗,以及是否存在另一种以原子方式执行upsert的方法?

Does anyone know why rules are not allowed in this situation and if there is another way to perform the upsert in an atomic way?

编辑:正如Andriy M评论的问题原子UPSERT SQL Server 2005中的SQL Server 已经包含了大量有关原子更新的信息,因此在此不再重复讨论.相反,我只想知道为什么MERGE语句不能在定义了规则的表上执行,并且有变通办法吗?

As Andriy M commented the question Atomic UPSERT in SQL Server 2005 already has a wealth of information on atomic upserts so I'll not duplicate the discussion here. Instead I'd like just to know why the MERGE statement cannot execute on tables with rules defined and is there a work-around?

推荐答案

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆