SQL Server 中的字符串连接
考虑这样一种情况,我们在 SQL Server 2005 的 SP 中有两个变量,如下所示,
@string1 = 'a,b,c,d'
@string2 = 'c,d,e,f,g'
是否有解决方案可以从中获取新字符串(如 (@string1 U @string2) )而不使用任何循环。即最终的字符串应该是这样的,
@string3 = 'a,b,c,d,e,f,g'
Consider a situation we have two variables in SQL Server 2005's SP as below,
@string1 = 'a,b,c,d'
@string2 = 'c,d,e,f,g'
Is there a solution to get a new string out of that like (@string1 U @string2) without using any loops. i.e the final string should be like,
@string3 = 'a,b,c,d,e,f,g'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您需要一组而不是一次一行地执行此操作。给定以下 split 函数:
然后使用下表和示例数据以及字符串变量,您可以通过这种方式获得所有结果:
结果:
在较新的版本 (SQL Server 2017+) 上,查询要简单得多,并且您不需要不需要创建您自己的自定义字符串分割函数:
现在,您真正应该做的是遵循前面的内容建议并首先将这些内容存储在相关表中。每当发生插入或更新时,您可以使用相同类型的拆分方法来单独存储字符串,而不是仅仅将 CSV 转储到单个列中,并且您的应用程序实际上不必更改将数据传递到的方式你的程序。但取出数据肯定会更容易!
编辑
为 SQL Server 2008 添加一个潜在的解决方案,该解决方案稍微复杂一些,但只需少一个循环即可完成工作(使用大量表扫描和替换)。我不认为这比上面的解决方案更好,而且它的可维护性肯定较差,但如果您发现自己能够升级到 2008 或更高版本(也适用于任何 2008+ 用户),那么它是一个可以测试的选项。遇到这个问题)。
代码:
这在 2005 年要做起来要困难得多(尽管并非不可能),因为您需要将
VALUES()
子句更改为UNION ALL
...In case you need to do this as a set and not one row at a time. Given the following split function:
Then with the following table and sample data, and string variable, you can get all of the results this way:
Results:
On newer versions (SQL Server 2017+), the query is much simpler, and you don't need to create your own custom string-splitting function:
Now that all said, what you really should do is follow the previous advice and store these things in a related table in the first place. You can use the same type of splitting methodology to store the strings separately whenever an insert or update happens, instead of just dumping the CSV into a single column, and your applications shouldn't really have to change the way they're passing data into your procedures. But it sure will be easier to get the data out!
EDIT
Adding a potential solution for SQL Server 2008 that is a bit more convoluted but gets things done with one less loop (using a massive table scan and replace instead). I don't think this is any better than the solution above, and it is certainly less maintainable, but it is an option to test out should you find you are able to upgrade to 2008 or better (and also for any 2008+ users who come across this question).
The code:
This is much trickier to do in 2005 (though not impossible) because you need to change the
VALUES()
clauses toUNION ALL
...有两种方法可以做到这一点:
构建一个 CLR 函数来为您完成这项工作。将逻辑移回 .NET 代码,这是更容易进行字符串操作的平台。
如果您必须使用 SQL Server,那么您将需要:
将两个字符串“分解”为两个表,此函数可能会有所帮助: http://blog.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx
获取两个表中的唯一字符串列表。 (简单查询)
将两个字符串表“内爆”到一个变量中(http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string)
Two ways you can do that:
Build a CLR function to do the job for you. Move the logic back to .NET code which is much easier platform for string manipulation.
If you have to use SQL Server, then you will need to:
"explode" the two strings into two tables, this function might help: http://blog.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx
Get a unique list of strings from the two tables. (simple query)
"implode" the two string tables into a variable (http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string)
找到此函数 dbo 相关答案中的.Split,您可以像这样使用this:
最后一个 SELECT 返回
Found this function dbo.Split in a related answer, which you can use like this:
The last SELECT returns
抱歉
,不清楚您只想要独特的事件。您使用什么版本的 SQL Server?字符串操作函数因版本而异。
如果您不介意 UDF 分割字符串,请尝试以下操作:
以及使用 UDF 的代码
How about
Sorry, wasn't clear you wanted only unique occurrences. What version of SQL server are you using? String manipulation functions vary per version.
If you don't mind a UDF to split the string, try this:
and the code to use the UDF
下面的 SQL 函数会将逗号分隔的列表转换为表变量...
然后您可以将两个表合并在一起,就像这样...
这将为您提供一个结果集
:
乙
c
d
e
f
克
The following SQL function will convert a comma separated list to a table variable...
You can then union the two tables together, like so...
Which will give you a result set of:
a
b
c
d
e
f
g