计算传递给 SQL 过程的参数数量

Count number of parameters passed to a procedure in TSQL

我有一个允许可变数量参数的程序,即

CREATE Procedure OutputProcedure
 @FirstName nvarchar(20) = null,
 @MiddleName nvarchar(20) = null, 
 @LastName nvarchar(20) = null, 
 @City nvarchar(20) = null,
 @AveragePercentage int out

AS
BEGIN
/*CODE*/
End

现在,我希望能够计算传递给此过程本身内部过程的参数的数量。我该怎么做?

我找到了 This,但它使用正在检查的过程的名称来检查参数,如果我只在同一过程中进行计数,我认为情况不应该如此。

您可以在 SP 中执行此操作。试试这个。

Declare @cnt int

select @cnt = case when @FirstName is not null then 1 else 0 End+
       case when @MiddleName is not null then 1 else 0 End+
       case when @LastName is not null then 1 else 0 End+
       case when @City is not null then 1 else 0 End 

Select @cnt