Using SQL Server 2005 CONTAINSTABLE and FREETEXTTABLE with a variable

I've been doing a bunch of work with SQL Server 2005 Full-Text Search lately. 

Let's say you're working on an application to manage books in a library and you've defined a full-text search index on the LibraryEntry table's Title column.

Here's a simple sample for how you'd search for all library entries that contain the word “car”. 

SELECT entry.Id, entry.Title, fulltextSearch.Rank
FROM 
LibraryEntry entry
JOIN
FreeTextTable(LibraryEntry, [Title], 'car') fulltextSearch
ON
entry.Id = fulltextSearch.[KEY]

That's pretty easy -- just pass 'car' into the FreeTextTable() statement and you're good.  While we're at it, let's pretend that that statement just returned 100 matching rows. 

Ok.  So what if you want to run this query inside of a stored procedure and you want to use a variable for the search string?  The documentation says that it'd be perfectly fine to do that.

CREATE PROCEDURE SearchByTitle
@search nvarchar
AS

SELECT entry.Id, entry.Title, fulltextSearch.Rank
FROM 
LibraryEntry entry
JOIN
FreeTextTable(LibraryEntry, [Title], @search) fulltextSearch
ON
entry.Id = fulltextSearch.[KEY]

Run this by issuing “EXEC SearchByTitle @search = 'car'” and you should get back 100 records just like the previous query, right? 
Wrong.  You get back zero records.  You don't get any errors or warnings.  You just get back nothing.

The solution: change the variable declaration from “nvarchar” to “nvarchar(1024)”. 

Here's the final working version of that stored procedure:

CREATE PROCEDURE SearchByTitle
@search nvarchar(1024)
AS

SELECT entry.Id, entry.Title, fulltextSearch.Rank
FROM 
LibraryEntry entry
JOIN
FreeTextTable(LibraryEntry, [Title], @search) fulltextSearch
ON
entry.Id = fulltextSearch.[KEY]

BTW, this also works for the CONTAINSTABLE() command as well.

-Ben

posted @ Friday, June 15, 2007 5:05 AM

Print

Comments on this entry:

# Interesting Finds: June 15, 1007

Left by Jason Haley at 6/15/2007 7:39 AM
Gravatar

# SQL Server Backup Command with a Variable

Left by Benjamin Day Consulting, Inc.: The Blog at 8/13/2007 9:37 AM
Gravatar
SQL Server Backup Command with a Variable

# re: Using SQL Server 2005 CONTAINSTABLE and FREETEXTTABLE with a variable

Left by Clarke Pitney at 4/7/2008 6:11 PM
Gravatar
Benjamin,
Your tip worked exactly as advertised...thank you very much for the advice. I am curious if you know the reason why it works only with NVARCHAR(1024) as opposed to an NVARCHAR of any other size?

Thanks,
Clarke

# re: Using SQL Server 2005 CONTAINSTABLE and FREETEXTTABLE with a variable

Left by Dave at 5/1/2008 12:10 AM
Gravatar
Specifying the datatype without the size will result in the default size being in the case you had. it's 1.

CREATE PROCEDURE SearchByTitle
@search nvarchar
AS

In the above code, anything you pass in will be truncated to 1 character. When a single character is passed into FREETEXT, its ignored as noise.

Take a look at the following (run it as a query):
DECLARE @s nvarchar
SET @s = 'car'
PRINT @s

Prints out 'c'

Verses:
DECLARE @s nvarchar(50)
SET @s = 'car'
PRINT @s

Prints out 'car'

So your fix doesn't need to use 1024, just a size that is going to be higher than the string you want to send it.

e.g.
CREATE PROCEDURE SearchByTitle
@search nvarchar(50)
AS

Hope that helps.

# re: Using SQL Server 2005 CONTAINSTABLE and FREETEXTTABLE with a variable

Left by Naveen at 3/21/2009 12:44 AM
Gravatar
I am not getting the result for the below query even though i have the value "IT" in the column formattedresume in the table "Resume"

select * from containstable(Resume,[FormattedResume],N'("it")')

Please help me out

Your comment:



 (will not be displayed)


 
 
 
Please add 2 and 6 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910