Tip of the Day : Differences Between SET and SELECT When Assigning Variables

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - May 18, 2026

Differences Between SET and SELECT When Assigning Variables

There are 2 ways of assigning a value to a local variable previously created with the DECLARE @LocalVariable statement, namely using the SET and the SELECT statements.  To illustrate:

DECLARE @SETVariable INT, @SELECTVariable INT
SET @SETVariable = 1
SELECT @SELECTVariable = 2

Here are the differences between the SET and SELECT statements:

SET Statement
• ANSI standard for variable assignment
• Can only assign one variable at a time
• When assigning from a query and the query returns no result, SET will assign a NULL value to the variable.
• When assigning from a query that returns more than one value, SET will fail with an error.

SELECT Statement
• Non-ANSI standard when assigning variables.
• Can assign values to more than one variable at a time.
• When assigning from a query and the query returns no result, SELECT will not make the assignment and therefore not change the value of the variable.
• When assigning from a query that returns more than one value, SELECT will assign the last value returned by the query and hide the fact that the query returned more than one row.

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions