动态 SQL 逗号分隔值查询
[更新:使用 SQL Server 2005]
您好,我想要做的是使用逗号分隔的值列表 (ids) 查询我的存储过程以检索数据行。
我收到的问题是转换错误:
Conversion failed when converting the varchar value ' +
@PassedInIDs + ' to data type int.
我的 where 子句和错误中的语句是:
...
AND (database.ID IN (' + @PassedInIDs + '))
注意:database.ID 是 int 类型。
我正在关注这篇文章:
http://www .sql-server-helper.com/functions/comma-delimited-to-table.aspx
但由于错误而未完成。
在我的执行脚本中,我有:
...
@PassedInIDs= '1,5'
我在这里做错了什么吗? 感谢您的帮助。
[Update: Using SQL Server 2005]
Hi, what I want to do is query my stored procedure with a comma-delimited list of values (ids) to retrieve rows of data.
The problem I am receiving is a conversion error:
Conversion failed when converting the varchar value ' +
@PassedInIDs + ' to data type int.
The statement in my where-clause and error is:
...
AND (database.ID IN (' + @PassedInIDs + '))
Note: database.ID is of int type.
I was following the article at:
http://www.sql-server-helper.com/functions/comma-delimited-to-table.aspx
but did not complete because of the error.
In my execution script I have:
...
@PassedInIDs= '1,5'
Am I doing something wrong here?
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我强烈建议您使用该链接中的第二种方法。 创建一个用户定义的函数,将逗号分隔的字符串转换为表格,然后您可以轻松地从中进行选择。
如果你用 Google 搜索 Erland 和“Dynamic SQL”,他对其中的陷阱有很好的描述。
I would strongly suggest that you use the second method from that link. Create a user-defined function that turns your comma-delimited string into a table, which you can then select from easily.
If you do a Google on Erland and "Dynamic SQL" he has a good writeup of the pitfalls that it entails.
其一,您将一个字符串传递给 SQL 中的 IN 函数。 如果您回顾一下原始文章,您会发现它不是发出直接的 SQL 语句,而是构建一个
字符串
,它是要执行的 SQL 语句。For one, you are passing a string to the IN function in SQL. If you look back at the original article, you'll see that instead of issuing a direct SQL statement, it instead is building a
string
which is the SQL statement to execute.SQL 中没有字符串计算。 这个:
变成::
不会仅仅因为
@PassedInIDs
参数恰好包含'1,2,3'
而 。 甚至没有查看该参数,因为您拥有的只是一个包含" + @PassedInIDs + "
的字符串。 从语法上讲,这相当于:简而言之,您无法在 SQL 中执行您在此处尝试的操作。 但还有其他四种可能性:
IN(?,?,?,?,?,?,?,?,?,?)
,只填你需要的数量,将其他设置为NULLIN (@p1, @p2, ..., @p10)
。There is no string evaluation in SQL. This:
will not be turned to:
just because the
@PassedInIDs
parameter happens to contain'1,2,3'
. The parameter is not even looked at, because all you have is a string containing" + @PassedInIDs + "
. Syntactically, this is equivalent to:To make it short, you can't do what you attempt here in SQL. But there are four other possibilities:
IN (?,?,?,?,?,?,?,?,?,?)
, filling only as many as you need, setting the others to NULLIN (@p1, @p2, ..., @p10)
.我将创建一个 CLR 表值函数:
http://msdn.microsoft。 com/en-us/library/ms131103.aspx
在其中,您将解析字符串并执行到一组行的转换。 然后,您可以连接该表的结果,或使用 IN 来查看某个 id 是否在列表中。
I would create a CLR table-valued function:
http://msdn.microsoft.com/en-us/library/ms131103.aspx
In it, you would parse the string apart and perform a conversion to a set of rows. You can then join on the results of that table, or use IN to see if an id is in the list.
您需要像对待表格一样对待 ufn_CSVToTable。 所以你可以加入这个函数:
You need to treat ufn_CSVToTable like it's a table. So you can join the function:
我建议在 SQL 2005 中使用 XML 来实现此目的。有点庞大,但它可以更容易。 它允许您将 XML 选择到表中,然后可以连接或插入等。
如果您还没有看过,请查看 Sql Server 的 OPENXML()。
例如,您可以传递类似以下内容:
'12...'
然后使用:
这应该是一个开始
I suggest using XML for this in SQL 2005. Somewhat bulkier, but it can be easier. It allows you to select the XML into a table which can then be joined or inserted etc.
Look at Sql Server's OPENXML() if you haven't already.
For example, you could pass in something like:
'12...'
and then use:
That should be a start
这可以通过 Narayana 的文章中提到的 6 种方法来解决 将列表/数组传递给 SQL Server 存储过程< /a>
我最困难的实现是
-
this may be solved by 6 ways as mentioned in Narayana's article Passing a list/array to an SQL Server stored procedure
And my most strait forward implementation is
-
这是我发现和测试的:
这是函数调用的方式:
Here is what I have found and tested:
Here is how function Call: