Home Database Mysql Tutorial SQL Server 2012 中 Update FROM子句

SQL Server 2012 中 Update FROM子句

Jun 07, 2016 pm 05:39 PM
from server update

首先说明一下需求以及环境 创建Table1以及Table2两张表,并插入一下数据 USE AdventureWorks2012 ; GO GO GO decimal GO NOT GO 1 , 20.0 ),( 2 , 30.0 ); 1 , 0.1 ),( 2 , 0.2 ); 如何通过一个update语句直接使Table2得到以下结果(仅是奇葩业务需求,请勿

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

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

USE AdventureWorks2012;

GO

GO

GO

decimal

GO

NOT

GO

1, 20.0),(2, 30.0);

1, 0.1),(2, 0.2);

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

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

SELECT dbo.Table2.ColA,

dbo.Table2.ColB

FROM dbo.Table2

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

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来实现此项功能

    AS source (ColA,COlB)

ON (target.ColA = source.ColA)

WHEN MATCHED

= target.ColB + source.ColB

;

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

TRUNCATE TABLE table1;

2, 10.0), (1, 20.0),(1, 40.0),(2, 30.0);

 

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

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

Msg 8672, Level 16, State 1, Line 1

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Fix: Google Chrome update check failed with error code 3: 0x80040154 Fix: Google Chrome update check failed with error code 3: 0x80040154 Apr 13, 2023 pm 05:46 PM

Google Chrome is one of the most popular browsers in the world and many users prefer to use it as the default browser on their Windows PCs. Chrome offers a wide range of features that make the browsing experience enjoyable and effortless, and therefore, it remains one of the most trusted browsers. But just like any other browser, even Chrome has its own shortcomings, and it's equally prone to bugs and glitches when you need it most. One such error is Error Code 3: 0x80040154, which occurs while checking for Google Chrome updates. The error message reads "An error occurred while checking for updates. The update check failed to start (Error code 3: 0x80080005) or (Error

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

How to enable/disable VS Code automatic updates How to enable/disable VS Code automatic updates Apr 28, 2023 am 09:28 AM

If you are using Visual Studio Code (VSCode) and thinking about how to disable its automatic software updates and how to disable the automatic updates of its extensions, then read this article. If you don't use VSCode often, open the editor after a long period of time and want to enable automatic updates, this article will also guide you in doing so. Let us discuss in detail the different ways to enable or disable VSCode automatic updates. Table of Contents Method One: Use Settings to Enable/Disable VSCode Automatic Updates Step 1: Open VS Code and click the gear-shaped symbol in the lower left corner. Step 2: Click Settings in the list that appears. Step 3: Type update in the search bar and hit enter. Find Updates: Pattern 4

KDE Plasma 6.1 brings many enhancements to the popular Linux desktop KDE Plasma 6.1 brings many enhancements to the popular Linux desktop Jun 23, 2024 am 07:54 AM

After several pre-releases, the KDE Plasma development team unveiled version 6.0 of its desktop environment for Linux and BSD systems on 28 February, using the Qt6 framework for the first time. KDE Plasma 6.1 now comes with a number of new features t

Solution to high CPU usage of Microsoft compatibility telemetry Solution to high CPU usage of Microsoft compatibility telemetry Mar 16, 2024 pm 10:16 PM

When we use the win10 system, we sometimes encounter situations where the computer becomes stuck. Then when we check the background process, we find that a Microsoftcompatibilitytelemetry process takes up a particularly high amount of resources. So what is going on? Users can try to uninstall the third-party protection software and then try a clean boot to operate. Let this site carefully introduce to users the solution to the high CPU usage of Microsoftcompatibilitytelemetry. Solution to the high CPU usage of Microsoftcompatibilitytelemetry Method 1: Try after uninstalling the third-party protection software

How to use UPDATE statement in SQL How to use UPDATE statement in SQL Jun 02, 2023 pm 09:13 PM

SQLUPDATE statement The Update statement is used to modify the data in the table. The syntax is as follows: UPDATE table name SET column name = new value WHERE column name = certain value "Person" table: LastNameFirstNameAddressCityGatesBillXuanwumen10BeijingWilsonChamps-Elysees Update a column in a certain row UPDATEPerson SETFirstName="Fred" WHERELastName="Wilson" Result: LastNa

Fitbit Ace LTE receives major update with new games, contactless payment and other features Fitbit Ace LTE receives major update with new games, contactless payment and other features Aug 08, 2024 pm 09:39 PM

The Fitbit Ace LTE was officially launched in May, but is currently only available in the US. The smartwatch is aimed specifically at children, who can receive rewards for games through a more active lifestyle, while parents can always monitor their

In-depth exploration of the usage details of UPDATE in MySQL In-depth exploration of the usage details of UPDATE in MySQL Oct 11, 2022 pm 07:32 PM

In MySQL, you can use the UPDATE statement to modify and update data in one or more tables. The following article will help you explore the details of the use of UPDATE in MySQL. I hope it will be helpful to you.

See all articles