SQL Server Admin
T-SQL Articles

March 02, 2009

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



Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

0 Responses to "USE TOP Operator and Store Results in a Variable using SQL Server"
 

Copyright © 2009-2012 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions