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

前端 未结 4 1138
执笔经年
执笔经年 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条回答
  •  梦毁少年i
    2021-02-05 16:55

    You can use XML types. Here is a sample procedure:

    CREATE PROCEDURE udsp_VariableParams(@params XML)
    AS
    BEGIN
        DECLARE @vdoc INT
        EXEC sp_xml_preparedocument @vdoc OUTPUT, @params
        SELECT * FROM OPENXML(@vdoc, '/params/param', 1) WITH ([paramname] VARCHAR(40), [paramvalue] VARCHAR(150))
        EXEC sp_xml_removedocument @vdoc    
    END
    
    EXEC udsp_VariableParams 
      '
         
         
      '
    
    EXEC udsp_VariableParams 
      '
         
         
         
      '
    

提交回复
热议问题