MS SQL - Define and set your own variables

By xngo on August 6, 2019

In MS SQL server, you can define and set your own variables. This is useful when you want to reuse the result values to the next query.

Here is an example how to do it.

DECLARE @my_product_id INT, @my_product_name VARCHAR(255)
SET @my_product_id = 1234
SET @my_product_name = 'fruit'
 
SELECT @my_product_id, @my_product_name

Output

MS SQL - Declare output

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.