SQL Server全文搜索 - 获取总字段值

时间:2015-06-24 07:01:24

标签: sql sql-server contains

我们说我有下表" Addresses":

+----+-------------+---------------+------------------+
| ID | CompanyName |    Street     |     City         |
+----+-------------+---------------+------------------+
|  1 | Salvador    | Hollywood 123 | Paradise City    |
|  2 | Zer0        | Avenue 34     | Opportunity City |
+----+-------------+---------------+------------------+

如果我进行全文搜索,请执行以下操作:

SELECT * FROM Addresses WHERE CONTAINS(*, 'Salv')

是否可以回来

  • 列的名称,其中包含已创建的值(在此示例中,它将是" CompanyName")
  • 列的完整值,其中包含已创建的值(在此示例中,它将是" Salvador"

1 个答案:

答案 0 :(得分:1)

我可以建议:

 SELECT 
      *,
      CASE WHEN CONTAINS(CompanyName, 'Salv') THEN 'CompanyName'
           WHEN CONTAINS(Street, 'Salv') THEN 'Street'
           WHEN CONTAINS(City, 'Salv') THEN 'City'
      END As ColumnName,
      CASE WHEN CONTAINS(CompanyName, 'Salv') THEN CompanyName
           WHEN CONTAINS(Street, 'Salv') THEN Street
           WHEN CONTAINS(City, 'Salv') THEN City
      END As FullText
 FROM Addresses 
 WHERE CONTAINS(*, 'Salv')