tags:

views:

26

answers:

3

i'm trying to concatenate several columns from a persistent table into one column of a table variable, so that i can run a contains("foo" and "bar") and get a result even if foo is not in the same column as bar.

however, it isn't possible to create a unique index on a table variable, hence no fulltext index to run a contains.

is there a way to, dynamically, concatenate several columns and run a contains on them? here's an example:

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*"' )
A: 
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%'

Is it something like this.

Muhammad Kashif Nadeem
LIKE doesn't support AND or OR
noobsaibot
hehe, that'd do that, in the most simple case of one AND or OR; but think what you'll end up with having several ANDs and ORs ;) ... a huge PITA
noobsaibot
+1  A: 

You cannot use CONTAINS on a table that has not been configured to use Full Text Indexing, and that cannot be applied to table variables.

If you want to use CONTAINS (as opposed to the less flexible PATINDEX) you will need to base the whole query on a table with a FT index.

Alex K.
well, i wouldn't want to alter the search pattern. i suppose patindex doesn't understand the same patterns as contains. what could i use instead of contains and go with either table vars or temp tables?
noobsaibot
That "search engine" type syntax is only available with full text services.Why not put full text on description_table and use CONTAINSTABLE()
Alex K.
because that way '"foo" AND "bar"' wouldn't be found as they are in different columns of description_table.
noobsaibot
CONTAINSTABLE() searches the specific columns you specify (i.e can be more than 1 column in the expression)
Alex K.
that's true, but it doesn't search for '"foo" AND "bar"' in the combined value of the given columns. it searches in all the given columns sequentially.
noobsaibot
In that case, without hacking about with multiple CONTAINSTABLE() joins the only other way I can think of is to add a computed column of (foo+bar) and FT index that.
Alex K.
A: 

You can't use full text indexing on a table variable but you can apply the full text parser. Would something like this do what you need?

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

NB: There might well be a better way of using the parser but I couldn't quite figure it out. Details of it are at this link

Martin Smith
no idea, don't really know what the dm_fts_parser part does. however, it'd have to accept the ANDs and ORs in order to be usefull for me.
noobsaibot