Variable Assignment in UPDATE Statements

One commonly overlooked feature in T-SQL is the ability to assign values to local variables in the SET clause of the UPDATE statement. This capability can help improve query performance by reducing locking and CPU contention and reducing the number of statements required in a T-SQL batch.

The simplified syntax of the SET clause for assigning values to variables is as follows:

SET    { column_name = { expression | DEFAULT | NULL }      | @variable = expression      | @variable = column = expression [ ,...n ]    } [ ,...n ]

One common use of variable assignment in UPDATE statements is when you have a table that is used for storing and generating unique key values. To demonstrate ...

Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.