3

我有两个字符串作为@CountryLocationIDs 和@LocationIDs 值:

@CountryLocationIDs = 400,600,150,850,160,250
@LocationIDs1       = 600,150,900

然后我需要另一个变量中的输出为:

@LocationIDs = 400,600,150,850,160,250,900

任何人请帮忙...提前谢谢...

4

3 回答 3

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 运算符:

已编辑

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子句

于 2013-02-01T07:40:54.917 回答
3

您还可以将选项与动态管理功能sys.dm_fts_parser 一起
使用 在脚本执行之前,您需要检查全文组件是否已安装:

SELECT FULLTEXTSERVICEPROPERTY ('IsFulltextInstalled')

0 = 未安装全文。1 = 已安装全文。NULL = 无效输入或错误。

如果 0 = 全文未安装,那么这篇文章对您来说是必要的如何在 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)
于 2013-02-01T08:30:07.600 回答
0

将帮助您完成编程

你也可以参考这个

于 2013-02-01T07:40:04.667 回答