2017-08-03 51 views
0

我使用全文搜索在SQL Server中,我有这样一个查询:全文搜索多列获得其列匹配

SELECT [Column 12] AS Column_12 , 
     [Column 0] AS Column_0 , 
     [Column 13] AS Column_13 , 
     [Column 3] AS Column_3 , 
     [Column 4] AS Column_4 , 
     [Column 14] AS Column_14 , 
     [Column 5] AS Column_5 , 
     [Column 2] AS Column_2 
FROM dbo.allCountries 
WHERE CONTAINS (([Column 12], [Column 0], [Column 13], [Column 3], 
     [Column 4], [Column 14], [Column 5], [Column 2]), @Location) 

我需要的是让这列匹配搜索值。如果搜索匹配多个列,我需要获取他们的名称或索引。

+0

即使没有FullText,你所要求的在你有的结构中是不可能的......你将需要UnPivot列到一个Multi-Join样式表中,并从中获得你需要的信息 – GoldBishop

回答

0

根据您的要求:

select id, [column], value 
from (
    SELECT [id] 
     ,[column 0],[column 1] 
     ,[column 2],[column 3] 
     ,[column 4],[column 5] 
    FROM [dbo].[allcountries] 
    where contains (*, @location) 
) tmp UNPIVOT (
    value for 
    [column] in ([column 0],[column 1],[column 2],[column 3],[column 4],[column 5]) 
) up 
where value like '%' + @location + '%' 

这将返回id(身份,PK)字段,column名([柱#]),以及搜索到的值(@location)