Prior to SQL Server 2005, the value in the TOP clause was limited to literal values which meant that variables could not be used. Hence, the number of rows returned could not be variable/dynamic. Below is an example:
SELECT TOP 10 * FROM table1
New to SQL Server 2005 is the ability to use a variable in the TOP clause.
DECLARE @top INT
SET @top = 10
SELECT TOP (@top) *
This is pretty useful when there is a requirement to have a user determine the number of rows returned in a result set. Note that the variable must be between parantheses.
Post a Comment