SQL select data into a variable

First declare the variable the will hold the data:

declare @ref as nvarchar(50)

The following code will insert the field REF from the table DATA into the newly created variable:

SELECT top 1 @ref = ref
from  table.dbo.DATA
where date = @date

 

This value can now be used anywhere in the scode of the query.

Advertisements