将充满逗号分隔值的 varchar 传递给 SQL Server IN 函数
我有一个 SQL Server 存储过程,其中我想将一个充满逗号分隔值的 varchar
传递给 IN
函数。 例如:
DECLARE @Ids varchar(50);
SET @Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN (@Ids);
这当然行不通。 我收到错误:
将 varchar 值“1,2,3,5,4,6,7,98,234”转换为 int 数据类型时转换失败。
如何在不构建动态 SQL 的情况下完成此操作(或相对类似的操作)?
Duplicate of
Dynamic SQL Comma Delimited Value Query
Parameterized Queries with Like and In
I have a SQL Server Stored Procedure where I would like to pass a varchar
full of comma delimited values to an IN
function. For example:
DECLARE @Ids varchar(50);
SET @Ids = '1,2,3,5,4,6,7,98,234';
SELECT *
FROM sometable
WHERE tableid IN (@Ids);
This does not work of course. I get the error:
Conversion failed when converting the varchar value '1,2,3,5,4,6,7,98,234' to data type int.
How can I accomplish this (or something relatively similar) without resorting to building dynamic SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我认为一个非常简单的解决方案可能如下:
I think a very simple solution could be following:
我可以建议像这样使用
WITH
:I can suggest using
WITH
like this:如果不使用动态 SQL,您必须获取输入变量并使用 split 函数将数据放入临时表中,然后加入其中。
Without using dynamic SQL, you have to take the input variable and use a split function to put the data into a temp table and then join to that.
这里有很多答案,但补充一下我的两分钱,我认为 STRING_SPLIT 是解决此类问题的一种非常简单的方法:
Tons of answers here, but to add my two cents I think
STRING_SPLIT
is a very simple approach to this sort of problem:谢谢你的功能我使用了它.........................
这是我的例子
Thanks, for your function I Used IT........................
This is my EXAMPLE
如果您使用 SQL Server 2008 或更高版本,请使用表值参数; 例如:
If you use SQL Server 2008 or higher, use table valued parameters; for example:
已经有一段时间了,但我过去曾使用 XML 作为临时措施来完成此操作。
我不能为此承担任何责任,但恐怕我不再知道我从哪里得到这个想法:
Its been a while but I have done this in the past using XML as a interim.
I can't take any credit for this, but I'm afraid I no longer know where I got this idea from:
创建一个如下所示的表函数,该函数解析逗号分隔的 varchar 并返回一个可以与其他表进行内部联接的表。
===================================================
现在在您的代码中使用上面创建的表函数,函数的创建是数据库中的一次性活动,可以跨数据库使用,也可以在同一服务器上使用。
Create a table function like below which parse comma separated varchar and returns a table that can be inner joined with other tables.
=================================================
Now consume above created table function in your code,creation of function is one time activity in your database that can be used across databases as well on same server.
我之前已经编写了一个存储过程来演示如何执行此操作。
您基本上必须处理字符串。
我试图在这里发布代码,但格式变得很糟糕。
I've written a stored procedure to show how to do this before.
You basically have to process the string.
I tried to post the code here but the formatting got all screwy.
我和用户KM有同样的想法。 但不需要额外的表号。 就只有这个功能。
这是测试:
结果:
I have same idea with user KM. but do not need extra table Number. Just this function only.
And here is the test:
Result:
您可以这样做:
然后尝试使用
您也可以在其他情况下将此 IN-Clause 与 regexp_substr 一起使用,只需尝试一下即可。
You could do it like this:
Then try it with
You can use this IN-Clause with regexp_substr in other situations too, just try it.
使用以下代码片段在 SQL Server 2014 中修复了上述错误
The above errorr was fixed in SQL Server 2014 by using following snippet
试试这个:
正如
Try this:
As said on the last example of this link
这个简单的答案适用于 SQL 2016 标准
This simple answer is for SQL 2016 standard
最好又简单的方法。
Best and simple approach.
工作小提琴在这里找到 Fiddle
working fiddle find here Fiddle
我遇到了同样的问题,并且我不想在源数据库上留下任何痕迹 - 即没有存储过程或函数。 我是这样处理的:
I ran into the same issue, and I don't want to have any footprint on the source database - i.e. no stored procedures or functions. I went about it this way:
@RBarryYoung(上面)的答案对我有用。
但是,当逗号分隔的字符串值之间有空格时,它将省略带空格的 ID。 所以我删除了空格。
看看下面的代码片段。
The answer by @RBarryYoung (above) worked for me.
But when you have spaces in between the comma separated string values, then it will omit IDs with spaces. So I removed the spaces.
Take a look at the code snippet below.
正如上面马特所说的那样,它如下所示(修正了一点)并且它正在工作:
As told by Matt above done it like below(Rectified little bit) and it's working:
我发现最简单的方法是使用 FIND_IN_SET
The simplest way i found was to use FIND_IN_SET
这对于我不想使用 CTE 并且也不想使用内部联接的要求之一很有用。
分割字符串的函数:
This came in handy for one of my requirements where I did not want to use CTE and also did not want to go with the inner join.
Function for split string :
当然,如果你像我一样懒,你可以这样做:
Of course if you're lazy like me, you could just do this:
不要使用循环分割字符串的函数!,我下面的函数将非常快地分割字符串,无需循环!
在使用我的函数之前,您需要设置一个“辅助”表,每个数据库只需执行一次:
使用此函数来分割字符串,它不会循环并且非常快:
您可以使用此函数作为联接中的表:
这是您的示例:
Don't use a function that loops to split a string!, my function below will split a string very fast, with no looping!
Before you use my function, you need to set up a "helper" table, you only need to do this one time per database:
use this function to split your string, which does not loop and is very fast:
you can use this function as a table in a join:
Here is your example:
无表无函数无循环 基于
我们的 DBA 建议使用 XML 将列表解析为表的想法。
这些似乎与 @KM 的答案具有相同的性能,但我认为简单得多。
No Table No Function No Loop
Building on the idea of parsing your list into a table our DBA suggested using XML.
These seems to have the same performance as @KM's answer but, I think, a lot simpler.
您可以创建一个返回表的函数。
所以你的陈述将类似于“
这是一个模拟函数”。
You can create a function that returns a table.
so your statement would be something like
Here is a simular function.
这是一个非常常见的问题。 罐头答案,几个不错的技术:
http://www.sommarskog.se/arrays- in-sql-2005.html
It's a very common question. Canned answer, several nice techniques:
http://www.sommarskog.se/arrays-in-sql-2005.html
这非常有效! 下面的答案太复杂了。 不要将其视为动态的。 按如下方式设置您的商店程序:
This works perfectly! The below answers are too complicated. Don't look at this as dynamic. Set up your store procedure as follows: