存储过程执行出错

时间:2011-12-28 08:56:39

标签: sql-server stored-procedures

此查询引发错误:

DECLARE @CategoryId int
SELECT @CategoryId = 1

exec [USP_GetLimitedRecords]
        @Query = N'SELECT b.* FROM Boxes b INNER JOIN Categories c ON b.CategoryId = c.Id WHERE c.Id= ' + @CategoryId,
        @PrimaryKey = N'id',
        @PageNo = 1,
        @PageSize = 20

错误是:

  

Msg 102,Level 15,State 1,Line 2'+'附近的语法不正确。

此查询有效

exec [USP_GetLimitedRecords]
        @Query = N'SELECT b.* FROM Boxes b INNER JOIN Categories c ON b.CategoryId = c.Id WHERE c.Id= 1',
        @PrimaryKey = N'id',
        @PageNo = 1,
        @PageSize = 20

1 个答案:

答案 0 :(得分:3)

请您尝试使用以下查询,谢谢

DECLARE @CategoryId int
DECLARE @MyQuery NVARCHAR(250)
SELECT @CategoryId = 1

SET @MyQuery = N'SELECT b.* FROM Boxes b INNER JOIN Categories c ON b.CategoryId = c.Id WHERE c.Id= ' + CAST( @CategoryId AS VARCHAR)

exec [USP_GetLimitedRecords]
        @Query = @MyQuery,
        @PrimaryKey = N'id',
        @PageNo = 1,
        @PageSize = 20