Monday, October 27, 2014

Can SELECT TOP have argument?

Newbie’s question, I believe.


I wanted to make a stored procedure to returns last several rows or last several hundred rows depending on the situation, so I started writing queries in order to copy/paste them into procedure. There are two queries, first works, but second one shows syntax error in select statement. Can anyone tell me why, and how to go around this?



SELECT TOP 5 * FROM Table1 ORDER BY ID DESC




DECLARE @Counter as int
SET @ Counter = 5
SELECT TOP @ Counter FROM Table1 ORDER BY ID DESC


No comments:

Post a Comment