SQL Server SELECT TOP into variable


You need to select the TOP 1 into a variable in SQL Server. In other words you need to read the first row of a table and for one of the columns in that first row you need to read it and store the contents into a variable.

Normally the code when you are not using TOP is SELECT @myVariable = [myColumn] FROM myTable.

When you are using TOP you need to put that right after the SELECT keyword as shown below.

SELECT TOP 1 @myVariable = [myColumn] FROM [myTable]

Leave a comment

Your email address will not be published. Required fields are marked *