C# SQL Top as parameter

muhan picture muhan · Aug 14, 2009 · Viewed 12.2k times · Source

Trying to parameterize the value of TOP in my sql statement.

SELECT TOP @topparam * from table1

command.Parameters.Add("@topparam",SqlDbType.VarChar, 10).Value = somevalue.ToString();

This doesn't seem to work. Anyone have any suggestions?
Just to clarify, I don't want to use stored procedures.

Answer

Cade Roux picture Cade Roux · Aug 14, 2009

In SQL Server 2005 and above, you can do this:

SELECT TOP (@topparam) * from table1