2010-08-03 65 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