TOP N作为存储的proc参数而不使用动态SQL?

fdk*_*fsf 0 t-sql sql-server stored-procedures

我有一个简单的存储过程,返回前20个更糟糕的百分比.SP看起来像这样:

Create procedure dbo.usp_GetAwfulSalesmen
as

select top 20 AvgSales, Name from Sales order by AvgSales ASC
Run Code Online (Sandbox Code Playgroud)

但是,假设我想要返回前20名之外的东西.

有没有办法将前N个发送到存储过程,以便它是一个参数而不是固定值?

我不想使用动态sql.

谢谢.

Joh*_*tti 7

将INT参数添加到存储过程(即@Top)

Select Top (@Top) AvgSales, Name 
 From Sales 
 Order by AvgSales ASC
Run Code Online (Sandbox Code Playgroud)