具有可变数量参数的存储过程
我有一个存储过程,我必须在其中传递参数,但问题是我不确定将有多少个参数,它可以是 1,在下次运行中可以是 5。
cmd.Parameters.Add(new SqlParameter("@id", id)
任何人都可以帮助我如何传递这些可变数量的参数存储过程中的参数? 谢谢
I have stored procedure where I have to pass parameters, But the problem is I am not sure how many parameters is going to come it can be 1, in next run it can be 5.
cmd.Parameters.Add(new SqlParameter("@id", id)
Can anyone help how can I pass these variable number of parameters in stored procedure?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将其作为逗号分隔的列表传递,然后使用 split 函数,并根据结果进行连接。
现在是您的存储过程:
然后调用它:
您可以在此处查看一些背景、其他选项和性能比较:
但是,在 SQL Server 2016 或更高版本上,您应该查看
STRING_SPLIT()
和STRING_AGG()
:You could pass it in as a comma-separated list, then use a split function, and join against the results.
Now your stored procedure:
Then to call it:
You can see some background, other options, and performance comparisons here:
On SQL Server 2016 or above, though, you should look at
STRING_SPLIT()
andSTRING_AGG()
:SQLServer 允许您将
TABLE
参数传递给存储过程。因此,您可以定义表类型,CREATE TYPE LIST_OF_IDS AS TABLE (id int not null Primary key)
,更改您的过程以接受这种类型的变量(它应该是只读的)。SQLServer lets you pass
TABLE
parameter to the stored procedure. So you can define table type,CREATE TYPE LIST_OF_IDS AS TABLE (id int not null primary key)
, alter your procedure to accept a variable of this type (it should be readonly).存储过程支持可选参数。与 C# 4 一样,您可以使用
=
指定默认值。例如:对于您不想传递的参数,请将它们设置为
null
或根本不将它们添加到cmd.Parameters
中。它们将在存储过程中具有默认值Stored procedures support optional parameters. Like C# 4, you can specify a default value using
=
. For example:For parameters you don't want to pass, either set them to
null
or don't add them tocmd.Parameters
at all. They will have their default value in the stored procedure您是否考虑过使用 字典 来实现此目的?
它将允许您将任意数量的参数作为键值对传递。
然后你只需要浏览字典并将这些参数添加到 cmd 中。
在存储过程本身中,您需要指定参数的默认值。
Have you considered using dictionary for that purpose?
It will allow you to pass any number of parameters as key-value pairs.
Then you'll need just to go through the dictionary and add those parameters to cmd.
In stored procedure itself you'll need to specify default values for the parameters.
下面是一个基于
,
作为分隔符分割字符串的代码片段。您甚至可以参数化逗号。它对于还没有String_split
函数的系统很有用:Here is a code snippet that splits a string based on
,
as delimiter. You can even parametrize the comma. It's useful on systems that don't have theString_split
function yet:另一种方法是将参数序列化为 JSON 字符串并将其传递到存储过程中。您可以使用 NewtonSoft.JSON 包执行以下操作:
string json = JsonConvert.SerializeObject(obj);
并将json
var 传递到您的过程中。Another way you could do this is by serializing the arguments as a JSON string and passing that into your stored procedure. You can use the NewtonSoft.JSON package to do something like this:
string json = JsonConvert.SerializeObject(obj);
and pass thejson
var into your proc.如果您有 Sql Server 2008 或更高版本,则可以使用表值参数...
https://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/
if you have Sql Server 2008 or later, you can use a table valued parameter...
https://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/