How to pass a variable number of parameters to a SQL Server stored procedure?

前端 未结 4 1194
执笔经年
执笔经年 2021-02-05 16:32

I used SQL Server 2005 for my small web application. I Want pass parameters to SP . But there is one condition. number of parameter that can be change time to time. Think ,this

4条回答
  •  Happy的楠姐
    2021-02-05 16:35

    You can set the default values for the parameters at the SP level so that the parameters become optional.

    e.g.

    CREATE PROCEDURE dbo.Test
     @param1 int, -- Not an optional
     @param2 int = 10, --Optional
     @param3 bit = 0, --Optional
     @param4 varchar(50) = '', --Optional
     @param5 nvarchar(100) = null --Optional
     -- more parameters ..... 
    AS
    BEGIN
    
     -- The SQL goes here...
    
    END
    

    Then you can populate the optional parameters at your choice.

提交回复
热议问题