HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Get the would-be row count without running twice

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
withoutthetwicewouldgetrunningcountrow

Problem

So I have a stored proc that utilizes a paging function to only return a subset of the query along with the actual total row count.
In other words, it takes parameters for the start and end rows to slice, and through an Out parameter, should return the total rows that are capable of being selected, along with returning the selected data.

So far, the only effective solution I've found is to run the query twice, once with the row-limiting settings in place and once without, just to execute the count(*) function to get that total.

The current windowing function uses a CTE as the main part of the query, but I'm fiddling with temp tables, and everything I try to simplify the query results in the script becoming slower.

Is this really the best approach?

```
--Tables

CREATE TABLE [dbo].tbl_DCCDR_OpenRequests NOT NULL,
[ACCTNO] decimal NOT NULL,
[AltAcctNo] varchar NULL,
[APPLCT] varchar NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].tbl_HIST NOT NULL,
[RefreshRunDT] [int] NOT NULL,
[CFOFileOrderDT] [int] NULL,
[ACCTNO] [int] NOT NULL,
[AltAcctNo] varchar NOT NULL,
[APPLCT] varchar NULL,
[CFOOrderReasonCode] varchar NOT NULL,
[DCCDRID] varchar NOT NULL,
[CFOReqstType] [smallint] NOT NULL,
[ActiveException] [bit] NOT NULL,
[FileOrderCloseDT] [int] NULL,
[FileDestructionDT] [int] NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].tbl_FileOrderEscalation,
[FileOrderID] [int] NOT NULL,
[EscalationOpenedBy] varchar NOT NULL,
[EscalationOpenedDate] [datetime] NOT NULL,
[EscalationClosedBy] varchar NULL,
[EscalationClosedDate] [datetime] NULL,
[Note] varchar NULL,
) ON [PRIMARY]

-- =============================================
-- Create basic stored procedure template
-- =============================================

-- Drop

Solution

Check out my answer on what is probably a duplicate question:

Find out beforehand how many records a query has

There is a link in that answer to another answer I posted on a related question on Stack Overflow.

So for the moment you have a few options, and I am researching another option as well.

Context

StackExchange Database Administrators Q#104353, answer score: 2

Revisions (0)

No revisions yet.