且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在以逗号分隔的文本列中查找唯一值

更新时间:2022-12-10 07:51:45

忽略所有评论中提到的表设计的明显问题,并接受这在大表上可能会证明这可能会很慢,这就是我可能会这样做的方法.

Ignoring the obvious problems with your table design as alluded to in all the comments and accepting that this might prove very slow on a huge table here's how I might do it.

首先...我将创建一个语句,将所有行转换为一个巨大的逗号分隔列表.

First... I would create a statement that would turn all the rows into one big massive comma delimited list.

DECLARE @tmp VarChar(max)
SET @tmp = ''
SELECT @tmp = @tmp + ColumnA + ',' FROM TableA

然后使用此 SO 文章中描述的表值 udf 拆分将大量字符串转回带有不同子句的表,以确保它是唯一的.

Then use the table valued udf split described by this SO article to turn that massive string back into a table with a distinct clause to ensure that it's unique.

https://***.com/a/2837662/261997

SELECT DISTINCT * FROM dbo.Split(',', @tmp)