T-SQL Parameterized SELECT TOP @N
If you wanted to select the TOP N rows from a table based on a stored procedure parameter you would likely have tried:
SELECT TOP @N * FROM MyTable
Having come across the error:
Incorrect syntax near '@N'
You would next have hit google and, like me, found a lot of FUD surrounding performing what you would think would be a relatively simple operation. You would have found talk about dynamic SQL, and no doubt all kinds of other hacks to get around it. However, from SQL 2005, you can do this:
SELECT TOP (@N) * FROM MyTable
It's as simple as that. Another good example where the highest ranked result isn't necessarily the right one.

0 Comments:
Post a Comment
<< Home