从MSSQL Server中的存储过程返回表和标量

时间:2016-11-30 16:16:29

标签: sql sql-server stored-procedures

我想通过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;

0 个答案:

没有答案