1

我正在尝试使用 Contains() 方法根据传递到存储过程的 TSQL 参数搜索全文索引的查询。Contains 需要进行 prefix_search,因为它将用于匹配部分单词(例如策略编号的前 5 个字符)。

这是我的查询。

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

但是当我尝试运行它时,企业管理器会响应错误:“+”附近的语法不正确

在这一点上,我不确定我可能做错了什么。

4

1 回答 1

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 (column, '"text**"'). 星号匹配零个、一个或多个字符(词根词或词或短语中的词)。如果文本和星号没有用双引号分隔,则谓词读取 CONTAINS (column, 'text*'),全文搜索将星号视为字符并搜索与 的完全匹配text*。全文引擎将找不到带有星号 ( *) 字符的单词,因为分词器通常会忽略此类字符。

于 2012-04-19T23:53:50.460 回答