Below is a simplified version of my sql query that uses CONTAINSTABLE for full text searching.
DECLARE @pSearchFor AS NVARCHAR(100);
SET @pSearchFor = 'SomeKeyword';
SELECT MS.[ModuleScreenID] AS ScreenID
,MS.[ModuleScreenCode] AS ScreenCode
,M.[Description] AS ModuleDescription
,M.[ModuleCode] AS ModuleCode
,FT.[Rank]
FROM ModuleScreen MS
JOIN Module M ON MS.ModuleID = M.ModuleID
JOIN CONTAINSTABLE(ModuleScreen, *, @pSearchFor) FT ON MS.ModuleScreenID = FT.[KEY]
I want to pass empty or null value for @pSearchFor parameter so that all records are returned by full text search. But I get a "Null or empty full-text predicate" error when I pass empty or null value. After googling, I found that CONTAINSTABLE cannot take an empty parameter for keywords. I have also seen this question in SO but it did not help me.
Can I make a conditional join with CONTAINSTABLE (only when a value is specified for @pSearchFor parameter)?. I am not sure how to achieve this. Would appreciate any pointers.
What do you expect to get when you search for an empty or null value? do you expect the query to return nothing or do you expect it to return something else.
If you want it to return nothing then you're best of doing something like this:
edit: fixed to now return all records when null or empty string is supplied.
If you have more than 2 CONTAINS table queries with different search strings, then I would suggest you generate the query using dynamic SQL as it would be much easier to maintain than a 2^n chain of almost the same queries
Edit: having looked at a way to do this without using multiple copies by using a temp table like this:
This should give you what you want without having to duplicate things, however you might want to restrict the result that is fed into the #temp table some more as it will get slow for larger tables.