USE TOP Operator and Store Results in a Variable using SQL Server

A user had a query where he wanted to use the TOP Operator and store the results of this operation in a variable to be used later. Here's how to do so:

I am using the 'Customers' table of the Northwind database


DECLARE @CustomerID nchar(5)


SELECT TOP 1 @CustomerID = CustomerID


From Customers


Where Country = 'Germany'


ORDER BY CustomerID DESC


 


PRINT @CustomerID


No comments:

Post a Comment