如何将可变数量的参数传递给SQL Server存储过程?

时间:2021-07-15 08:51:12

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 time i pass name and Address , next time i pass name,surname,address ,

我使用SQL Server 2005作为我的小型Web应用程序。我想将参数传递给SP。但有一个条件。可以随时更改的参数数量。想想,这次我通过姓名和地址,下次我通过姓名,地址,

this parameter range may be 1-30 ,

此参数范围可能是1-30,

4 个解决方案

#1


12  

You declare the procedure with default parameters and you invoke it with named parameters instead of positional parameters:

使用默认参数声明过程,并使用命名参数而不是位置参数调用它:

CREATE PROCEDURE usp_myProcedure
  @name varchar(100) = '',
  @surname varchar(100) = '',
  @address varchar(100) = ''
AS
BEGIN
...
END

to invoke it from T-SQL:

从T-SQL调用它:

exec usp_myProcedure @name='John', @surname = 'Doe';
exec usp_myProcedure @name='Jane', @address = '123 Anystreet';

To invoke it from C#:

要从C#调用它:

SqlCommand cmd = new SqlCommand('usp_MyProcedure', ...);
cmd.CommandType = commandtype.StoredProcedure;
cmd.Parameters.AddWithValue('@name', 'John');
cmd.Parameters.AddWithValue('@surname', 'Doe');

#2


10  

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

您可以使用XML类型。这是一个示例程序:

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 
  '<params>
     <param paramname="name" paramvalue="value"/>
     <param paramname="address" paramvalue="value"/>
  </params>'

EXEC udsp_VariableParams 
  '<params>
     <param paramname="name" paramvalue="value"/>
     <param paramname="surname" paramvalue="value"/>
     <param paramname="address" paramvalue="value"/>
  </params>'

#3


6  

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

您可以在SP级别设置参数的默认值,以使参数成为可选参数。

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.

然后,您可以根据自己的选择填充可选参数。

#4


1  

Preparing and exctracting tags from XML is aperformance killer. The inconsistant execution timings depends on whether plan is cached or not.

从XML准备和提取标签是一种性能杀手。不一致的执行时间取决于计划是否被缓存。

Using NVARCHAR(MAX) instead is a better option. Just prepare you parameter strings with "@name=Value" pair, and add a unique parameter sepearetor if needed. Inside the procedure use SUBSTRING, CHARINDEX, etc. to get individual parameters.

相反,使用NVARCHAR(MAX)是一个更好的选择。只需使用“@ name = Value”对准备参数字符串,并根据需要添加唯一参数sepearetor。在程序内部使用SUBSTRING,CHARINDEX等来获取单个参数。

#1


12  

You declare the procedure with default parameters and you invoke it with named parameters instead of positional parameters:

使用默认参数声明过程,并使用命名参数而不是位置参数调用它:

CREATE PROCEDURE usp_myProcedure
  @name varchar(100) = '',
  @surname varchar(100) = '',
  @address varchar(100) = ''
AS
BEGIN
...
END

to invoke it from T-SQL:

从T-SQL调用它:

exec usp_myProcedure @name='John', @surname = 'Doe';
exec usp_myProcedure @name='Jane', @address = '123 Anystreet';

To invoke it from C#:

要从C#调用它:

SqlCommand cmd = new SqlCommand('usp_MyProcedure', ...);
cmd.CommandType = commandtype.StoredProcedure;
cmd.Parameters.AddWithValue('@name', 'John');
cmd.Parameters.AddWithValue('@surname', 'Doe');

#2


10  

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

您可以使用XML类型。这是一个示例程序:

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 
  '<params>
     <param paramname="name" paramvalue="value"/>
     <param paramname="address" paramvalue="value"/>
  </params>'

EXEC udsp_VariableParams 
  '<params>
     <param paramname="name" paramvalue="value"/>
     <param paramname="surname" paramvalue="value"/>
     <param paramname="address" paramvalue="value"/>
  </params>'

#3


6  

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

您可以在SP级别设置参数的默认值,以使参数成为可选参数。

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.

然后,您可以根据自己的选择填充可选参数。

#4


1  

Preparing and exctracting tags from XML is aperformance killer. The inconsistant execution timings depends on whether plan is cached or not.

从XML准备和提取标签是一种性能杀手。不一致的执行时间取决于计划是否被缓存。

Using NVARCHAR(MAX) instead is a better option. Just prepare you parameter strings with "@name=Value" pair, and add a unique parameter sepearetor if needed. Inside the procedure use SUBSTRING, CHARINDEX, etc. to get individual parameters.

相反,使用NVARCHAR(MAX)是一个更好的选择。只需使用“@ name = Value”对准备参数字符串,并根据需要添加唯一参数sepearetor。在程序内部使用SUBSTRING,CHARINDEX等来获取单个参数。