2011-12-28 173 views
1

此查詢引發錯誤:錯誤在存儲過程的執行

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 Incorrect syntax near '+'.

此查詢的工作

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 
+2

你不能加入字符串設置呼叫參數 – 2011-12-28 08:59:25

+3

順便說一句,這看起來是一個可怕的方式來解決這個問題(即傳入的動態查詢)。 – 2011-12-28 09:00:04

+0

查看答案不要很快地說號碼試着找到備用或解決方案 – 2011-12-28 10:10:14

回答

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