如何将以下存储过程中由星号标记的最内层SELECT语句的计数作为输出参数返回?
Create procedure MySp
AS
Select RowNumber,NewsId From
(
Select Row_Number() Over (ORDER BY FirstVisit) as 'RowNumber',NewsId From
(
* Select Distinct NewsId,FirstVisit,PublishDate From VwNewsPack
) as t
) as tt
Where NewsId between 10 and 20
答案 0 :(得分:2)
你不能一体化。也就是说,都分配给变量并选择结果集。这是另一个。
您可以返回一个额外的列:
Select RowNumber,NewsId,InnerCount From
(
Select Row_Number() Over (ORDER BY FirstVisit) as 'RowNumber', NewsId
, COUNT(*) OVER () AS InnerCount
From
(
Select Distinct NewsId,FirstVisit,PublishDate From VwNewsPack
) as t
) as tt
Where NewsId between 10 and 20
如果这还不够好,那么你需要一个内部选择的临时表,并指定@@ ROWCOUNT作为变量