pagination - Better way for Getting Total Count along with Paging in SQL Server 2012 -
i have requirement total count of records along paging. @ present doing listed below in sql server 2012. needs separate query getting count. there improved way in sql server 2012?
alter procedure dbo.tpgetpagerecords ( @offsetrowno int, @fetchrowno int, @totalcount int out ) select cstno, cstabbr dbatabc cstabbr 'a%' order cstno offset ( @offsetrowno-1 ) * @fetchrowno rows fetch next @fetchrowno rows set @totalcount = (select count(*) dbatabc cstabbr 'a%') go
if we're allowed change contract, can have:
select cstno, cstabbr,count(*) on () totalcount dbatabc cstabbr 'a%' order cstno offset ( @offsetrowno-1 ) * @fetchrowno rows fetch next @fetchrowno rows
and total available separate column in result set. unfortunately, there's no way assign value variable in same statement, can no longer provide out
parameter.
this uses over
clause (available since 2005) allow aggregate computed on entire (unlimited) result set , without requiring group
ing.
Comments
Post a Comment