views:

228

answers:

1

I need to build a sqlserver query that if a text column has a full text index, it will be used, if not, the query will degrade to the O(N²) approach of LIKE '%word%'?

I believe the answer will be something like:

IF has_full_text_index('mycolumn')
    select mytable_id from mytable where contains(mycolumn, 'word')
ELSE
    select mytable_id from mytable where mycolumn like '%word%'
ENDIF

The query will be generated by a program. The query will always have just one word to match. The test to see if a column has a full text index must be quick.

+1  A: 

While I tend to agree with Ken that you will know which of your tables belongs to a fulltext catalog, you can query

sp_help_fulltext_tables {catalogname}

for the tables in your catalog. (via MSDN)

Jim H.
sp_help_fulltext_tables fails if the Full-Text Search isn't enabled for the database
neves
SELECT fulltextserviceproperty('IsFulltextInstalled')will return 1 or 0 depending upon whether FullText is running.
Jim H.