2010-08-03 30 views
3

我試圖將持久表中的多個列連接成表變量的一列,以便我可以運行一個包含(「foo」和「bar」)和即使foo與bar不在同一列,也會得到結果。包含對錶變量或臨時表的搜索

但是,無法在表變量上創建唯一索引,因此無法運行包含的全文索引。

有沒有辦法動態地連接幾個列並在其上運行一個包含?這裏有一個例子:

declare @t0 table 
(
    id uniqueidentifier not null, 
    search_text varchar(max) 
) 

declare @t1 table (id uniqueidentifier) 

insert into 
    @t0 (id, search_text) 
select 
    id, 
    foo + bar 
from 
    description_table 

insert into 
    @t1 
select 
    id 
from 
    @t0 
where 
    contains(search_text, '"c++*" AND "programming*"') 
+0

你是什麼版本的SQL Server? – 2010-08-03 15:29:31

+0

sql server 2008 sp1 – noobsaibot 2010-08-03 15:39:58

回答

2

對於尚未配置爲使用全文索引並且不能應用於表變量的表,不能使用CONTAINS

如果您想使用CONTAINS(而不是靈活性較低的PATINDEX),您需要將整個查詢基於FT索引表。

+0

好吧,我不想改變搜索模式。我想patindex不理解與包含相同的模式。我可以使用什麼而不是包含並使用表變量或臨時表? – noobsaibot 2010-08-03 15:17:40

+0

「搜索引擎」類型語法僅適用於全文服務。 爲什麼不把全文放在description_table上,並使用CONTAINSTABLE() – 2010-08-03 15:24:40

+0

,因爲這種方式''foo'和'bar''不會被找到,因爲它們在description_table的不同列中。 – noobsaibot 2010-08-03 15:26:44

0
declare @table table 
(
    id int, 
    fname varchar(50) 
) 

insert into @table select 1, 'Adam James Will' 
insert into @table select 1, 'Jain William' 
insert into @table select 1, 'Bob Rob James' 

select * from @table where fname like '%ja%' and fname like '%wi%' 

是不是這樣的。

+0

LIKE不支持AND或OR – noobsaibot 2010-08-03 15:19:35

+0

hehe,這樣做,在一個AND或OR最簡單的情況下;但想想你會得到幾個AND和OR;)...一個巨大的PITA – noobsaibot 2010-08-03 18:49:40

1

不能在表變量上使用全文索引,但可以應用全文分析器。這樣的事情會做你需要的嗎?

declare @d table 
(
id int identity(1,1), 
testing varchar(1000) 
) 

INSERT INTO @D VALUES ('c++ programming') 
INSERT INTO @D VALUES ('c# programming') 
INSERT INTO @D VALUES ('c++ books') 


SELECT id 
FROM @D 
CROSS APPLY sys.dm_fts_parser('"' + REPLACE(testing,'"','""') + '"', 1033, 0,0) 
where display_term in ('c++','programming') 
GROUP BY id 
HAVING COUNT(DISTINCT display_term)=2 

注意:可能有更好的方法來使用解析器,但我無法弄清楚。它的詳細信息是at this link

+0

不知道,不知道dm_fts_parser部件是幹什麼的。然而,它必須接受AND和OR,以便對我有用。 – noobsaibot 2010-08-03 18:46:23