CTE Documentation

  • I've always understood that CTEs were similar to derived tables in that they were simply logical constructs that the optimizer would turn into a query plan. In explaining CTEs, the analogy I use most often is that they are like a view that persists only for the duration of the single query in which it is contained. The use of a CTE does not, by itself, require any tempdb resources. A query plan might spill over to tempdb on a sort or something, but not merely because a CTE was used in the query.

    While I hold these truths to be self-evident, I've just spent an hour or so online trying to find any statement to this effect in BOL. I can find articles and questions answered in forums, but nothing from Microsoft itself. I've also taken a look at the SQL Server 2008 Internals manual but can't find anything.

    Could someone point me in the right direction for documentation that a CTE is like an inline view?

    P.S. I will be continuing my search and will post an answer if I find it first.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • I typically use exactly the same analogies (non-persistent VIEW or derived tables) but unfortunately I can't be helpful and point you to a link that agrees with us. 🙂

    However I recently found out that CTEs are supported in Oracle SQL. So I wonder if the analogy might be presented in some sort of ISO/ANSI standards documentation on the topic (assuming there is an ISO or ANSI standard for CTEs).


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thanks, Dwain. Here's the only thing close I've found so far. I didn't realize it was in there until i retreated to the 2005 new features. The text below is copied from

    2008 R2 Using Common Table Expressions

    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. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query....

    A CTE can be used to:

    ...

    Substitute for a view when the general use of a view is not required; (Emphasis mine)

    ...

    Does anyone know of any other authority to add to that?

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Wiki seems to support us:

    Common Table Expressions


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thanks again, Dwain. I'll keep looking in the morning.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply