需要varchar参数的SQL Server存储过程无法按预期工作

时间:2018-01-18 02:56:09

标签: sql sql-server stored-procedures

我对SQL很新,所以请原谅任何无知的问题。

这是我的存储过程:

ALTER PROCEDURE [dbo].[search_highlights]
    @page INT = 1,
    @recsPerPage INT = 20,
    @query NVARCHAR(MAX) = ''
AS
BEGIN
    SET NOCOUNT ON

    DECLARE @FirstRec AS INT = (@page - 1) * @recsPerPage,
            @LastRec AS INT = (@page * @recsPerPage + 1)

    SELECT
        [id],
        [datetime],
        [player_ids], [player_names],
        [headline], [duration],
        [blurb], [bigblurb],
        [thumb_s], [thumb_m], [thumb_l],
        [video_s], [video_m], [video_l],
        [gameid],
        [team_id], [team_name]
    FROM 
        (SELECT
             [id],
             [datetime],
             [player_ids], [player_names],
             [headline],
             [duration],
             [blurb], [bigblurb],
             [thumb_s], [thumb_m], [thumb_l],
             [video_s], [video_m], [video_l],
             [gameid],
             [team_id], [team_name],
             ROW_NUMBER() OVER (ORDER BY datetime DESC) AS rn
         FROM 
             [bbt].[dbo].[highlights]
         WHERE 
             CONTAINS((player_names, headline, blurb, bigblurb, team_name), @query)
        ) hp
    WHERE 
        rn BETWEEN @FirstRec AND @LastRec
    ORDER BY 
        datetime DESC
END

我试图像这样调用存储过程:

EXEC [dbo].[search_highlights] 1, 20, '"double play"'

当我运行时,我收到此错误:

  

Msg 7630,Level 15,State 2,Procedure dbo.search_highlights,Line 13 [Batch Start Line 0]
  “全文搜索条件”中的“”附近的语法错误。“

当我在没有"的情况下尝试时,错误消失了,但即使我得到它们也没有结果。

帮助?

1 个答案:

答案 0 :(得分:0)

在SQL Server中,您需要为字符串定义长度(大多数上下文中的默认值为1)。

因此,将存储过程定义为:

ALTER PROCEDURE [dbo].[search_highlights] (
    @page int = 1,
    @recsPerPage int = 20,
    @query VARCHAR(max) = ''
) AS . . .