2017-08-03 53 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)