我正试图获得一张桌子前10名,前5名的记录。因此,我将创建一个带有参数的存储过程,该参数接受记录数作为参数。但它在查询中给@number附近的错误。
这是我试过的,这给了我一个语法错误:
错误=消息102,级别15,状态1,过程selectVoterTopGivenNumberRECS,第10行'@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;
如何克服这个问题?
答案 0 :(得分:3)
您需要在parameter
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';
答案 1 :(得分:0)
您只需要向变量添加参数,更改
SELECT TOP @number
的
SELECT TOP (@number)