当前位置:   article > 正文

SQL Server 2012 中 Update FROM子句

update or delete the same

首先说明一下需求以及环境

创建Table1以及Table2两张表,并插入一下数据

USE AdventureWorks2012;

GO

IF OBJECT_ID ('dbo.Table1', 'U') IS NOT NULL

DROP TABLE dbo.Table1;

GO

IF OBJECT_ID ('dbo.Table2', 'U') IS NOT NULL

DROP TABLE dbo.Table2;

GO

CREATE TABLE dbo.Table1

(ColA int NOT NULL, ColB decimal(10,3) NOT NULL);

GO

CREATE TABLE dbo.Table2

(ColA int PRIMARY KEY NOT NULL, ColB decimal(10,3) NOT NULL);

GO

INSERT INTO dbo.Table1 VALUES(1, 20.0),(2, 30.0);

INSERT INTO dbo.Table2 VALUES(1, 0.1),(2, 0.2);

如何通过一个update语句直接使Table2得到以下结果(仅是奇葩业务需求,请勿吐槽)

可以看出请产生的结果集如使用SELECT 仅需使用简单INNER JOIN来获得

SELECT dbo.Table2.ColA,

dbo.Table2.ColB

FROM dbo.Table2

INNER JOIN dbo.Table1

ON (dbo.Table2.ColA = dbo.Table1.ColA);

而对于SQL Server 因为UPDATE 具有From子句 可以写出如下语句,但强烈不建议使用的此方法来进行

UPDATE dbo.Table2

SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB

FROM dbo.Table2

INNER JOIN dbo.Table1

ON (dbo.Table2.ColA = dbo.Table1.ColA);

官方解释如下

Use caution when specifying the FROM clause to provide the criteria for the update operation. The results of an UPDATE statement are undefined if the statement includes a FROM clause that is not specified in such a way that only one value is available for each column occurrence that is updated, that is if the UPDATE statement is not deterministic.

指定 FROM 子句为更新操作提供条件时务须小心。 如果 UPDATE 语句包含了未指定每个更新列的位置只有一个可用值的 FROM 子句(换句话说,如果 UPDATE 语句是不确定性的),则其结果将不明确。

Support for use of the READUNCOMMITTED and NOLOCK hints in the FROM clause that apply to the target table of an UPDATE or DELETE statement will be removed in a future version of SQL Server. Avoid using these hints in this context in new development work, and plan to modify applications that currently use them.

在 SQL Server 的未来版本中,将不再支持在 FROM 子句中使用应用于 UPDATE 或 DELETE 语句目标表的 READUNCOMMITTED 和 NOLOCK 提示。 请避免在新的开发工作上下文中使用这些提示,并计划修改当前使用它们的应用程序。

 

个人建议使用MERGE来实现此项功能

MERGE dbo.Table2 AS target

USING (SELECT ColA,ColB FROM dbo.Table1)

    AS source (ColA,COlB)

ON (target.ColA = source.ColA)

WHEN MATCHED

THEN UPDATE SET target.ColB = target.ColB + source.ColB

;

有兴趣的同学可以将Table1的数据更改如以下

TRUNCATE TABLE table1;

INSERT INTO dbo.Table1 VALUES(2, 10.0), (1, 20.0),(1, 40.0),(2, 30.0);

 

使用Update后Table2的结果(注意这是一个未确定性的结果集,不代表可以运行处结果结果一定是正确的)

使用MERGE是结果,这个错误信息很好的阐述的这个错误的原因

Msg 8672, Level 16, State 1, Line 1

The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.

 

Oracle 部分,有空我写一下

是错误的,Oracle中(仅在12c中测试)不允许update多行值对于多行列的更新

http://www.cnblogs.com/JasonLiao/archive/2009/12/23/1630895.html

 

Update解释

http://msdn.microsoft.com/en-us/library/ms177523.aspx

FROM子句

http://msdn.microsoft.com/en-us/library/ms177523.aspx#OtherTables

MERGE 解释

http://msdn.microsoft.com/en-us/library/bb510625.aspx

Oracle 中Update

http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_10008.htm#SQLRF01708

转载于:https://www.cnblogs.com/cnLing/p/3316178.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/969345
推荐阅读
相关标签
  

闽ICP备14008679号