2010-09-16 35 views
3

我正在創建一個如下所示的過程。當沒有「TOP @Count」時它可以正常工作,或者當我將「TOP 100」設置爲具體值時它工作正常。不能用戶「選擇TOP @Count ...」

那麼,爲什麼我不能通過那裏的價值?我怎麼走動它?

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE PROCEDURE MyProcedure  
    @Count int = 100 
AS 
BEGIN 

    SELECT TOP @Count 
     t1.id AS ID, 
     t1.name AS Name, 
     t2.type AS TYPE  
    FROM sampleTable1 as t1 with (noloack), 
     sampleTable2 as t2 with (noloack)   
    WHERE (t1.t2Id = t2.Id)  
ORDER BY t1.name asc 

END 
GO 

回答

2

假設2005+,你需要使用括號:

SELECT TOP (@Count) 
     t1.id AS ID, 
     t1.name AS Name, 
     t2.type AS TYPE 
    FROM sampleTable1 as t1 with (noloack) 
    JOIN sampleTable2 as t2 with (noloack) ON t2.id = t1.t2.id 
ORDER BY t1.name 

我的理解是爲了不需要dynamic SQLbracket support was added in v2005