2010-09-23 49 views
3

比方說,我有這樣的SQL查詢:爲什麼在CONTAINS語句中不能調用ISNULL?

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

如果我嘗試執行,我得到一個錯誤,指出「附近有語法錯誤ISNULL'。」我知道這是ISNULL的正確語法。是否有理由不能在CONTAINS語句中調用ISNULL?

回答

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)

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

因爲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如上所述,單引號之間,不是一個表達。

1

作爲一個猜測,我會說因爲包含用於全文搜索而不是常規的t-SQl,所以常規的t-sql函數不會在其中工作。 @KM發佈了我想要嘗試的東西。

相關問題