我有这样的查询:
SELECT id, @RowNumber = ROW_NUMBER() OVER (ORDER BY id) as 'number'
FROM WaitList
WHERE email = @Email
然而我收到错误:
Incorrect syntax near the keyword 'as'.
我要做的是将ROW_NUMBER()分配给变量。
我真的需要从WaitList表中获取行号。
我可以将结果放在变量表中,然后通过选择“数字”来获取行号吗?
答案 0 :(得分:1)
您的代码看起来像SQL Server。您无法分配变量和返回具有相同查询的结果集:
SELECT id, ROW_NUMBER() OVER (ORDER BY id) as number
FROM WaitList
WHERE email = @Email;
将值赋值给变量并不是很有意义。如果你想要,比如匹配行的总数,那么使用聚合函数:
select @cnt = count(*)
from waitlist
where email = @email;
答案 1 :(得分:1)
如果您想要具有匹配电子邮件地址的行号,但该表实际上没有行号,那么您应创建一个CTE来分配行号,然后从中选择特定行......这样的事情:
WITH NumberedRows AS (
SELECT [id], [email], ROW_NUMBER() OVER (ORDER BY id) as [Number]
FROM WaitList
)
SELECT @RowNumber = Number
FROM NumberedRows
WHERE email = @Email
请注意,如果在此之前有SELECT语句,则需要确保使用分号终止SELECT语句(或其他SQL语句)。或者,如果您愿意,您可以在WITH前加上一个分号,例如&#34 ;; WITH ..."
答案 2 :(得分:0)
select a.id , a.number
from
(SELECT id, ROW_NUMBER() OVER (ORDER BY id) as number
FROM WaitList
WHERE email = @Email) a
答案 3 :(得分:-1)
SELECT id, ROW_NUMBER() OVER ( ORDER BY id) as number
FROM WaitList
WHERE email = @Email;