CONTAINS function and OR
- From: "Edward Forgacs" <edward.forgacs@xxxxxxxxxxxxxxx>
- Date: Fri, 20 Jan 2006 09:38:58 +1100
I am attempting to create a query with a "dynamic" CONTAINS query, e.g.
DECLARE @Keywords VARCHAR(128)
SET @Keywords = NULL
SELECT * FROM Jobs WHERE (@Keywords IS NULL OR CONTAINS(JobTitle,
@Keywords))
However, the CONTAINS function seems to behave very weirdly when OR is
involved. Even though @Keywords is null, it still evaluates the contains,
and fails because of the null.
Server: Msg 7603, Level 15, State 1, Line 40
Syntax error in search condition, or empty or null search condition ''.
On the other and, if I change the query to this:
SELECT * FROM Jobs WHERE (@Keywords IS NULL OR CONTAINS(JobTitle,
@Keywords)) AND JobID IN (SELECT JobID FROM JobSkills WHERE SkillID = 2)
It works fine.
But that's not it. If I move the order of the clauses:
SELECT * FROM Jobs WHERE JobID IN (SELECT JobID FROM JobSkills WHERE SkillID
= 2) AND (@Keywords IS NULL OR CONTAINS(JobTitle, @Keywords))
I get the original error. Same goes for adding another contains clause at
the beginning. It seems that the original CONTAINS works fine, others defy
all logic.
Can someone suggest what is causing this behaviour (or why it works like
this - doesn't seem to make any sense) and a possible workaround short of
using hideous dynamic SQL?
.
- Follow-Ups:
- Re: CONTAINS function and OR
- From: Daniel Crichton
- Re: CONTAINS function and OR
- Prev by Date: Re: Identify/parse content within documents?
- Next by Date: Re: CONTAINS function and OR
- Previous by thread: Searching for multiple words in one field
- Next by thread: Re: CONTAINS function and OR
- Index(es):
Relevant Pages
|
Loading