Better way for Getting Total Count along with Paging in SQL Server 2012

LCJ

I have requirement to get the total count of records along with paging. At present I am doing it as listed below in SQL Server 2012. This needs a separate query for getting count. Is there any improved way in SQL Server 2012?

ALTER PROCEDURE dbo.tpGetPageRecords
(
    @OffSetRowNo INT,     
    @FetchRowNo INT,
    @TotalCount INT OUT
) 
AS 

SELECT CSTNO, CSTABBR 
FROM DBATABC
WHERE CSTABBR LIKE 'A%'
ORDER BY CSTNO
OFFSET ( @OffSetRowNo-1 ) * @FetchRowNo ROWS
FETCH NEXT @FetchRowNo ROWS ONLY

SET @TotalCount = 
(SELECT COUNT(*)
FROM DBATABC
WHERE CSTABBR LIKE 'A%')


GO
Damien_The_Unbeliever

If we're allowed to change the contract, you can have:

SELECT CSTNO, CSTABBR,COUNT(*) OVER () as TotalCount
FROM DBATABC
WHERE CSTABBR LIKE 'A%'
ORDER BY CSTNO
OFFSET ( @OffSetRowNo-1 ) * @FetchRowNo ROWS
FETCH NEXT @FetchRowNo ROWS ONLY

And now the total will be available as a separate column in the result set. Unfortunately, there's no way to assign this value to a variable in this same statement, so we can no longer provide it as an OUT parameter.

This uses the OVER clause (available since 2005) to allow an aggregate to be computed over the entire (unlimited) result set and without requiring GROUPing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server 2012 paging and subquery

Dynamic PIVOT table SQL Server 2012 with COUNT() and SUM() and a TOTAL column

Getting count of total documents when using aggregation along with skip and limit

Is there a better way to count frequencies of getting in the intreval?

SQL Server: COUNT by group and total

Running total (COUNT) SQL Server

GET total COUNT of all the count in SQL Server

Getting distinct count in SQL Server

SQL Server 2012/2014 Calculate running count

Total Count of column value in Sql Server

Better way of getting the count of an object's property in a collection

Group by with count along with total count in all the rows

Better way to use SQL Server function?

Prevent Help Make Microsoft SQL Server 2012 Better message

Is there a way how to get total count of entities of given type in CRM without paging

Better way to create nested array with count equals to children's total count

SQL Server 2012 - Running Total With Backlog & Carry Forward

SQL Server 2012 - Reset Running Total Based on the Value of Another Column

SQL Server: select count of rows with not empty fields and total count of rows

Sql ,query for getting total count joining 2 tables

MS SQL SERVER PAGING

Paging SQL Server results

Getting Return Count of Select SQL Server

Getting datetime count range in SQL Server

Calculate Date Range count for each day SQL Server 2012

How to count row in SQL SERVER 2012 using sys.partitions

How to get COUNT(*) from one partition of a table in SQL Server 2012?

Return Employee Count > 1 MDX SQL Server 2012

XML method for an attribute is getting NULL value in SQL Server 2012

TOP Ranking

HotTag

Archive