【发布时间】:2014-12-16 11:01:23
【问题描述】:
我需要从表中获取前 10、前 100 条这样的记录。但我需要在变量中为 top 子句分配值,例如 10 或 100。
但是当我像下面这样给出时,它给出了一个语法错误“'@numberofRecords'附近的语法不正确。”。
DECLARE @numberofRecords INT
SELECT @numberofRecords = ConfigValue FROM tblConfigItems (NOLOCK) WHERE configName = 'TopRecords'
SELECT TOP @numberofRecords [ID],TypeID, MessageType, operationDate
FROM NotifyTbl (NOLOCK)
wHERE STATUS IN ('1', '2')
如何做到这一点?
【问题讨论】:
标签: sql-server stored-procedures declare clause