C# SQL Top 作为参数

2022-01-23 00:00:00 sql tsql sql-server ado.net

试图在我的sql语句中参数化TOP的值.

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.

推荐答案

在SQL Server 2005 及更高版本,您可以这样做:

SELECT TOP (@topparam) * from table1

相关文章