Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does SQL Server support using a column value to update another column, and also updating the first column, in the same statement?

For example:

UPDATE Table1 SET Column1 = Column2, Column2 = Column3

Is this guaranteed to always result in an update like this:

Before:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Column1 Column2 Column3
10 200 3000

After:

Column1 Column2 Column3
200 3000 3000

A quick experimentation suggests it works, but I couldn’t find anything on MSDN that definitively says it would.

>Solution :

Yes, the result will be as desired. SQL Server performs the update as a set-based operation so the update logically happens all at once. One can even reverse the column order to achieve the same results:

UPDATE Table1 SET Column1 = Column2, Column2 = Column3;
UPDATE Table1 SET Column2 = Column3, Column1 = Column2;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading