我有两个字符串@CountryLocationIDs和@LocationIDs值:
@CountryLocationIDs = 400,600,150,850,160,250
@LocationIDs1 = 600,150,900
然后我需要另一个变量的输出:
@LocationIDs = 400,600,150,850,160,250,900
任何人都请帮忙......提前致谢...
答案 0 :(得分:3)
我创建了表值函数,它接受两个参数,第一个是带ID的字符串,第二个是字符串中的分隔符。
CREATE FUNCTION [dbo].[Split](@String nvarchar(4000), @Delimiter char(1))
returns @temptable TABLE (items nvarchar(4000))
as
begin
declare @idx int
declare @slice nvarchar(4000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
创建函数后,只需使用UNION
set运算符:
<强> EDITED 强>
WITH ListCTE AS
(
select items from dbo.split('400,600,150,850,160,250', ',')
union
select items from dbo.split('600,150,900', ',')
)
SELECT TOP 1
MemberList = substring((SELECT ( ', ' + items )
FROM ListCTE t2
ORDER BY
items
FOR XML PATH( '' )
), 3, 1000 )FROM ListCTE t1
使用UNION
,您将自动从两个字符串中获取不同的值,因此您无需使用DISTINCT
子句
答案 1 :(得分:3)
您还可以使用带动态管理功能的选项sys.dm_fts_parser
在脚本执行之前,您需要检查是否已安装全文组件:
SELECT FULLTEXTSERVICEPROPERTY ('IsFulltextInstalled')
0 =未安装全文。 1 =已安装全文。 NULL =输入无效或错误。
如果0 =未安装全文,则此帖子对您How to install fulltext on sql server 2008?
是必要的DECLARE @CountryLocationIDs nvarchar(100) = '400,600,150,850,160,250',
@LocationIDs1 nvarchar(100) = '600,150,900',
@LocationIDs nvarchar(100) = N''
SELECT @LocationIDs += display_term + ','
FROM sys.dm_fts_parser('"'+ 'nn,' + @CountryLocationIDs + ',' + @LocationIDs1 + '"', 1033, NULL, 0)
WHERE display_term NOT LIKE 'nn%'
GROUP BY display_term
SELECT LEFT(@LocationIDs, LEN(@LocationIDs) - 1)
答案 2 :(得分:0)