我想通过SP的OUT参数返回表数据和表数据的计数。表数据不是最终状态的计数,而是表格数据预先做TOP 100只返回100行。我认为以下是我想要的,但有更有效的方法来实现这一目标吗?
CREATE PROC sp_total_recs
@total_recs INT OUT
AS
BEGIN
-- return top 100 records only
SELECT TOP 100 *
INTO #temp
FROM
(
SELECT *, COUNT(1) OVER () AS total_records
FROM table
) T;
SET @total_recs = (SELECT DISTINCT total_records FROM #temp);
SELECT *
FROM #temp;
DROP TABLE #temp;
RETURN
END;
DECLARE @tot_recs INT;
EXEC sp_total_recs
@total_recs = @tot_recs OUTPUT;