Monday, September 16, 2013

How to parameterize the value of a Top in a SQL query?

Supposing that you have a query like 'Select TOP x from table' and you want to parameterize x param value(in a C# method) then there is a trick which you must have in consideration if you want the query to run.
So, in Sql server 2005 and above you can use parenthesis before and after you param.
Here you go:
 

SELECT TOP (@elements) * from tableCities


It is pretty easy, isn't it?

No comments:

Post a Comment