views:

311

answers:

3

I have a very fat common table expression which includes row numbers so that I can return a paged result set. I also want to return the total number of records that match the query before I page the result set.

with recs as (select *, row_number() over (order by id) as rownum from ......)
select * from recs where rownum between @a and @b .... select count(*) from recs

Obviously my query above is patchy, but it's just for illustrating my point. I want a page of results AND the total number of matches. How do I do this without having to literally copy and paste the entire 20+ line CTE?

+3  A: 

Don't think you can. From MSDN

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement.

Emphasis on "single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement."

This might be a situation where you want to use a Temporary Table.

CREATE TABLE #Recs
{
  .....
}
INSERT INTO #Recs
select *, row_number() over (order by id) as rownum from ......

If you don't know the structure of the table before hand you can use this form to create a temporary table:

select *, row_number() over (order by id) as rownum INTO #Recs from ......

You will be able to use the Temporary table in the manner you have described above.

Abe Miessler
Also, I'd recommend using those "SELECT *"s only if you truly need them. They can cause performance issues, and most of the time aren't really necessary.
Abe Miessler
This syntax for creating the temp table may also prove useful: Select * Into #Recs From...
David Hall
Actually I have a complex SELECT statement I need to do on hierarchical data and the way in which it is called will vary heavily depending on the situation.
Nathan Ridley
Hmmm, are you saying that the structure of the CTE/Temp table will vary? If that is the case then I would recommend David Hall's suggestion. That will allow you to define the structure of your temporary table based on what you select (similar to your CTE).
Abe Miessler
My issue with using a temp table is that I don't want to stuff half a million or more rows into a table. Seems inefficient to do it that way.
Nathan Ridley
A: 

A CTE can be used as many times as you like within the one query. But you could always make a view or inline TVF to reduce your typing, without needing to lose the performance benefit of paging with a temporary table.

Rob Farley
The CTE can only be referenced in the statement that immediately follows the CTE query, which means I can't run two unique select statements that reference the original CTE.
Nathan Ridley
Yes. A statement is a query. To handle multiple queries, consider an inline TVF to hold your functionality.
Rob Farley
Ok, but what's a TVF?
Nathan Ridley
Table-valued function
Rob Farley
+3  A: 

You can use commas to create multiple CTEs that references the CTEs Above.

Just to illustrate what I mean:

with recs as (
select 
    *, 
    row_number() over (order by id) as rownum from ......
    ),
counts as (
    select count(*) as totalrows from recs
)
select recs.*,count.totalrows
from recs
cross apply counts 
where rownum between @a and @b .... 

This is not the a good solution.

The best solution I found to have the total count in a CTE without counting the records is described in this article.

DECLARE @startRow INT ; SET @startrow = 50
;WITH cols
AS
(
    SELECT table_name, column_name, 
        ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq, 
        ROW_NUMBER() OVER(ORDER BY table_name DESC, column_name desc) AS totrows
    FROM [INFORMATION_SCHEMA].columns
)
SELECT table_name, column_name, totrows + seq -1 as TotRows
FROM cols
WHERE seq BETWEEN @startRow AND @startRow + 49
ORDERBY seq
Jose Chama
Yeah I thought of this, but there is an issue when the query returns no records. I guess I could fudge it with a UNION ALL and a dummy row...
Nathan Ridley
Check out the last piece of code I took from the article. What is does it has an ascending and descending row count and it just add them in the results to get the total number of rows. This performs really good in our production environments.
Jose Chama
Ahh brilliant! That link has a really good way to achieve this.
Nathan Ridley