我試圖獲得表格的前10位,前5位的記錄。所以我要創建一個存儲過程,其參數是接受記錄數作爲參數。但它在查詢中給我一個@number附近的錯誤。如何在存儲過程中插入頂部選擇號碼作爲參數
這就是我想,這給了我一個語法錯誤:
error=Msg 102, Level 15, State 1, Procedure selectVoterTopGivenNumberRECS, Line 10 Incorrect syntax near '@number'.
代碼:
CREATE PROCEDURE selectVoterTopGivenNumberRECS
(
@number int
)
AS
BEGIN
BEGIN TRANSACTION
SET NOCOUNT ON;
--Query
SELECT TOP @number
V.NICorPP as NICorPP,V.fName+' '+V.mName+' '+V.lName AS Name,
V.DOB AS DateofBirth,
CASE (V.gender)
WHEN 1 THEN 'Male'
WHEN 2 THEN 'Female'
END AS Gender,V.vtaddress AS VoterAddress,V.phHome AS ContactHome,
V.phMob AS ContactMobile,V.occupation AS Occupation,
V.occRegID AS RegisteredOCCID,LGD.LGDivName
FROM Voter V
INNER JOIN LGDivision LGD ON V.lgDiv=LGD.LGDivID
WHERE V.vtStatus='Active';
COMMIT TRANSACTION
END;
如何克服呢?
能否請您包括你收到的錯誤?看起來你並沒有完成那句話。 – paqogomez
@paqogomez error = Msg 102,Level 15,State 1,Procedure selectVoterTopGivenNumberRECS,Line 10 '@number'附近語法不正確。 – chathwind
請參閱:http://stackoverflow.com/questions/175962/dynamic-select-top-var-in-sql-server –