为什么不能在CONTAINS语句中调用ISNULL?

时间:2010-09-23 18:46:33

标签: sql tsql

假设我有这个SQL查询:

declare @input varchar(20)
select *
from myTable
where CONTAINS (myColumn, ISNULL(@input, 'replacement text'))

如果我尝试执行此操作,则会收到错误消息“ISNULL'附近的语法错误。”我知道这是ISNULL的正确语法。是否有理由不能在CONTAINS语句中调用ISNULL?

4 个答案:

答案 0 :(得分:5)

试试这个:

declare @input varchar(20)
SET @input=ISNULL(@input, 'replacement text')
select *
from myTable
where CONTAINS (myColumn, @input)

当您尝试将表达式放入存储过程调用时,这是同样的问题:

EXEC YourProcedure ISNULL(@input, 'replacement text') --will fail too

CONTAIN需要字符串文字,请参阅CONTAINS (Transact-SQL)

答案 1 :(得分:3)

在那里需要字符串文字,而不是表达式。

<强>更新

The code for the procedure is:

CREATE PROCEDURE freetext_rank_proc
       @select_list             nvarchar(1000),
       @from_table              nvarchar(517),
       @freetext_column         sysname,
       @freetext_search         nvarchar(1000),
       @additional_predicates  nvarchar(500)      = '',
       @order_by_list           nvarchar(500)      = ''
AS 
BEGIN
   DECLARE @table_id              integer,
           @unique_key_col_name   sysname,
           @add_pred_var          nvarchar(510),
           @order_by_var          nvarchar(510) 

   -- Get the name of the unique key column for this table.
   SET @table_id = Object_Id(@from_table)
   SET @unique_key_col_name = 
   Col_Name( @table_id, 
   ObjectProperty(@table_id, 'TableFullTextKeyColumn') )     

   -- If there is an additional_predicate, put AND() around it.
   IF @additional_predicates <> ''
      SET @add_pred_var = 'AND (' + @additional_predicates + ')'
   ELSE
      SET @add_pred_var = ''

   -- Insert ORDER BY, if needed.
   IF @order_by_list <> ''
      SET @order_by_var = 'ORDER BY ' + @order_by_var
   ELSE
      SET @order_by_var = ''

   -- Execute the SELECT statement.
   EXECUTE (   'SELECT ' 
             + @select_list
             + ' FROM '
             + @from_table
             + ' AS FT_TBL, FreetextTable('
             + @from_table
             + ','
             + @freetext_column
             + ','''
             + @freetext_search
             + ''') AS KEY_TBL '
             + 'WHERE FT_TBL.'
             + @unique_key_col_name
             + ' = KEY_TBL.[KEY] ' 
             + @add_pred_var
             + ' '
             + @order_by_var
           )
END

答案 2 :(得分:2)

因为syntax of the language不允许:

CONTAINS
   ( { column_name | ( column_list ) | * } 
     ,'<contains_search_condition>'     
   [ , LANGUAGE language_term ]
   ) 

<contains_search_condition> ::= 
    { <simple_term> 
    | <prefix_term> 
    | <generation_term> 
    | <proximity_term> 
    | <weighted_term> 
    } 
    | { ( <contains_search_condition> ) 
    [ { <AND> | <AND NOT> | <OR> } ] 
    <contains_search_condition> [ ...n ] 
    } 

正如您所看到的,语法只需要一个contains_search_condition如上所述,在单引号之间,而不是表达式。

答案 3 :(得分:1)

作为一个猜测,我会说因为包含用于全文搜索而不是常规t-SQl所以常规的t-sql函数不适用于它。 @KM发布了我试试的内容。