SQL 创建不同的逗号分隔列表
SQL create distinct comma separated lists
我有一个存储过程,它接受一个逗号分隔的列表,然后用带引号的字符串和括号复制该列表,然后在动态 sql 语句中使用它们来构建一个数据透视表 table具有灵活的列数。
我的问题是,有时我的用户会提交包含重复项的列表,这会导致数据透视查询失败。所以我想以某种方式 select 区别于我的逗号分隔字符串。
下面是我如何操作初始字符串:
Declare @data varchar(max) = '150593, 150593, 150603, 150578, 150604'
Declare @bracketed varchar(max) = ''
Declare @quoted varchar(max) = ''
select @bracketed = @bracketed + quotename(rtrim(ltrim(Value))) + ', ',
@quoted = @quoted + quotename(rtrim(ltrim(Value)), '''') + ', '
from [dbo].[fnSplitStringAsTable](@data, ',')
Select @bracketed = LEFT(@bracketed, len(@bracketed) - 1),
@quoted = LEFT(@quoted, len(@quoted) - 1)
我想我应该能够在此查询中的某处添加 DISTINCT,
但我无法让它发挥作用。我怎样才能 select 区别于逗号分隔的列表?
我想我们可以在制作 table 之后添加 distinct,如下所示:
select @bracketed = @bracketed + quotename(rtrim(ltrim(Value))) + ', ',
@quoted = @quoted + quotename(rtrim(ltrim(Value)), '''') + ', '
from (
SELECT DISTINCT Value FROM [dbo].[fnSplitStringAsTable](@data, ',')
) T
如果失败试试这个:
select @bracketed = @bracketed + quotename(Value) + ', ',
@quoted = @quoted + quotename(Value), '''') + ', '
from (
SELECT DISTINCT RTRIM(LTRIM(Value)) AS Value FROM [dbo].[fnSplitStringAsTable](@data, ',')
) T
使用一点动态 sql,您可以 select 将字符串变量中的不同值放入 table 变量中,然后将这些值放回原始变量中:
declare @data varchar(max) = '150593, 150593, 150603, 150578, 150604'
declare @table table(data varchar(10))
set @data = 'select distinct value from (values (''' +
replace(@data,', ','''),(''') + ''')) as v(value)'
insert into @table
exec(@data)
set @data = ''
select @data = @data + data +
case row_number() over(order by data desc)
when 1 then ''
else ','
end
from @table
order by data asc
select @data
作为替代解决方案,您可以在 xml 中进行重复数据删除并转换回 varchar
Declare @data varchar(max) = '150593, 150593, 150603, 150578, 150604'
set @data= (select '''' + cast(cast('<d>'+replace(@data, ', ',',</d><d>')+'</d>' as xml).query('distinct-values(/d)') as varchar) +'''')
select @data
我有一个存储过程,它接受一个逗号分隔的列表,然后用带引号的字符串和括号复制该列表,然后在动态 sql 语句中使用它们来构建一个数据透视表 table具有灵活的列数。
我的问题是,有时我的用户会提交包含重复项的列表,这会导致数据透视查询失败。所以我想以某种方式 select 区别于我的逗号分隔字符串。
下面是我如何操作初始字符串:
Declare @data varchar(max) = '150593, 150593, 150603, 150578, 150604'
Declare @bracketed varchar(max) = ''
Declare @quoted varchar(max) = ''
select @bracketed = @bracketed + quotename(rtrim(ltrim(Value))) + ', ',
@quoted = @quoted + quotename(rtrim(ltrim(Value)), '''') + ', '
from [dbo].[fnSplitStringAsTable](@data, ',')
Select @bracketed = LEFT(@bracketed, len(@bracketed) - 1),
@quoted = LEFT(@quoted, len(@quoted) - 1)
我想我应该能够在此查询中的某处添加 DISTINCT,
但我无法让它发挥作用。我怎样才能 select 区别于逗号分隔的列表?
我想我们可以在制作 table 之后添加 distinct,如下所示:
select @bracketed = @bracketed + quotename(rtrim(ltrim(Value))) + ', ',
@quoted = @quoted + quotename(rtrim(ltrim(Value)), '''') + ', '
from (
SELECT DISTINCT Value FROM [dbo].[fnSplitStringAsTable](@data, ',')
) T
如果失败试试这个:
select @bracketed = @bracketed + quotename(Value) + ', ',
@quoted = @quoted + quotename(Value), '''') + ', '
from (
SELECT DISTINCT RTRIM(LTRIM(Value)) AS Value FROM [dbo].[fnSplitStringAsTable](@data, ',')
) T
使用一点动态 sql,您可以 select 将字符串变量中的不同值放入 table 变量中,然后将这些值放回原始变量中:
declare @data varchar(max) = '150593, 150593, 150603, 150578, 150604'
declare @table table(data varchar(10))
set @data = 'select distinct value from (values (''' +
replace(@data,', ','''),(''') + ''')) as v(value)'
insert into @table
exec(@data)
set @data = ''
select @data = @data + data +
case row_number() over(order by data desc)
when 1 then ''
else ','
end
from @table
order by data asc
select @data
作为替代解决方案,您可以在 xml 中进行重复数据删除并转换回 varchar
Declare @data varchar(max) = '150593, 150593, 150603, 150578, 150604'
set @data= (select '''' + cast(cast('<d>'+replace(@data, ', ',',</d><d>')+'</d>' as xml).query('distinct-values(/d)') as varchar) +'''')
select @data