2012-04-19 6 views
1

私は、ストアドプロシージャに渡されたTSQLパラメータに基づいてフルテキストインデックスを検索するために、Contains()メソッドを使用するクエリに対処しようとしています。 Containsは、部分的な単語(ポリシー番号の最初の5文字など)の一致に使用されるため、prefix_searchを実行する必要があります。TSQLパラメータ化しました

これは私の質問です。

CREATE PROCEDURE [SearchMail] 
(
    @AccountId int, 
    @SearchTerm VARCHAR(100) 
) 
AS 

SET NOCOUNT ON 

    BEGIN 
     SELECT MailId, AccountId, ServerId, ToAddress, FromAddress, DateSent, DateReceived , 
         [Subject], Body, PolicyNumber, ProducerNumber, IsRead, IsDeleted, IsImaged, 
         DateUpdated, DateDeleted, DateImaged, UpdatedBy, DeletedBy, ImagedBy 
     FROM MailMessages 
     WHERE AccountId = @AccountId 
       AND CONTAINS(([Subject], Body, PolicyNumber, ProducerNumber, FromAddress), ' "' + @SearchTerm + '*" ') 
       AND IsDeleted = 0 
       AND IsImaged = 0 
    END 
GO 

しかし、私はこれを実行しようとすると、Enterprise Managerは、エラーで応答します。この時点で、不正な構文は近い「+」

、私は私が間違ってやってすることができるものをわからないと思います。

答えて

2
CREATE PROCEDURE [SearchMail] 
( 
    @AccountId int, 
    @SearchTerm VARCHAR(100) 
) 
AS 

SET NOCOUNT ON 

BEGIN 
    Declare @SearchWithWildcard VARCHAR(200) 
    SET @SearchWithWildcard = '"' + @SearchTerm + '*"' 

    SELECT MailId, AccountId, ServerId, ToAddress, FromAddress, DateSent, DateReceived, 
      [Subject], Body, PolicyNumber, ProducerNumber, IsRead, IsDeleted, IsImaged, 
      DateUpdated, DateDeleted, DateImaged, UpdatedBy, DeletedBy, ImagedBy 
    FROM MailMessages 
    WHERE AccountId = @AccountId 
      AND CONTAINS(([Subject], Body, PolicyNumber, ProducerNumber, FromAddress), 
         @SearchWithWildcard) 
      AND IsDeleted = 0 
      AND IsImaged = 0 
END 
GO 

参考CONTAINS

Specifies a match of words or phrases beginning with the specified text. Enclose a prefix term in double quotation marks ("") and add an asterisk (*) before the ending quotation mark, so that all text starting with the simple term specified before the asterisk is matched. The clause should be specified this way: CONTAINS (column, '"text**"') . The asterisk matches zero, one, or more characters (of the root word or words in the word or phrase). If the text and asterisk are not delimited by double quotation marks, so the predicate reads CONTAINS (column, 'text*') , full-text search considers the asterisk as a character and searches for exact matches to text* . The full-text engine will not find words with the asterisk (*) character because word breakers typically ignore such characters.

+0

私は検索語で始まるものを見つけようとしています。たとえば、検索用語が「CNM000」の場合、「CNM00」と「CNM0003245」に一致するようにしたいと思います –