2012-04-19 7 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 

는하지만, 엔터프라이즈 관리자 오류로 응답 : 잘못된 구문 가까운이 시점에서 "+"

, 나는 내가 잘못 일을 할 수 무엇을 확신입니다.

답변

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

은 내가 SEARCHTERM로 시작하는 것을 찾기 위해 노력하고있다. 예를 들어, 검색 용어가 'CNM000'인 경우 'CNM00'와 'CNM0003245' –