Is Full Text Search CONTAINS clause always evaluat

2019-07-20 12:59发布

问题:

Is there a way to avoid the error Null or empty full-text predicate in the example below ?

DECLARE @SearchText VARCHAR(1000);
SET @SearchText = ''

SELECT * FROM   myTable
WHERE
    /* 
    SOME CONTITIONS 
    AND
    */ 
    ( 
      @SearchText = ''
      OR
      (
        @SearchText <> ''
        AND
        CONTAINS((myField1, myField2), @SearchText)
      )
    )

I could be doing like this, but I want to avoid duplicating the code :

DECLARE @SearchText VARCHAR(1000);
SET @SearchText = ''

IF @SearchText = ''
BEGIN
  SELECT * FROM   myTable
  WHERE 
      /* 
      SOME CONTITIONS 
      */ 
END
ELSE
BEGIN
  SELECT * FROM   myTable
  WHERE
      /* 
      SOME CONTITIONS 
      AND
      */ 
      ( 
        @SearchText = ''
        OR
        (
          @SearchText <> ''
          AND
          CONTAINS((myField1, myField2), @SearchText)
        )
      )
END

[EDIT]

I found the answer here

So the solution is to set the @SearchText to '""' instead of leaving it empty.

回答1:

I found the answer here.

The solution is to set the @SearchText to '""' instead of leaving it empty.