﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Kev Riley  / Power and Deception of CTEs / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 23:42:20 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]timothyawiseman (12/6/2008)[/b][hr][quote][b]Jeff Moden (12/6/2008)[/b][hr][quote][b]timothyawiseman (12/5/2008)[/b][hr]To slightly modify a quote from Larry Wall, "[i]SQL[/i] is designed to give you several ways to do anything, so consider picking the most readable one."[/quote]I'm [i]definitely [/i]for that and glad to see anyone concerned about readability for a change... but even CTE's won't make readable code without a bit of concern for formatting and commenting.  Heh... I had one guy at my previous job that thought it was a good formatting choice to write each query on a single line and all lower case.[/quote]You have a point.  But, in my opinion, a well formatted CTE will be easier to read and understand than even a well formatted derived table inside some other portion of the command.[/quote]Heh... AGREED!  Ephasis on "well formatted".  You and I are saying the same thing!</description><pubDate>Sat, 06 Dec 2008 15:38:17 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>So application developer A writes the CTE -- or the derived table -- then application developer B writes it slightly differently a month later in another application.  Modularity?  No, maintenance nightmare.  There should be a DBA writing views, verifying with business analysts that the views select exactly what is expected, documenting the views, and reviewing the SQL written by the application developers to make sure they use standard views -- instead of developing as if their programming creativity is what it's about.</description><pubDate>Sat, 06 Dec 2008 15:35:01 GMT</pubDate><dc:creator>katesl</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Jeff Moden (12/6/2008)[/b][hr][quote][b]timothyawiseman (12/5/2008)[/b][hr]To slightly modify a quote from Larry Wall, "[i]SQL[/i] is designed to give you several ways to do anything, so consider picking the most readable one."[/quote]I'm [i]definitely [/i]for that and glad to see anyone concerned about readability for a change... but even CTE's won't make readable code without a bit of concern for formatting and commenting.  Heh... I had one guy at my previous job that thought it was a good formatting choice to write each query on a single line and all lower case.[/quote]You have a point.  But, in my opinion, a well formatted CTE will be easier to read and understand than even a well formatted derived table inside some other portion of the command.</description><pubDate>Sat, 06 Dec 2008 14:38:11 GMT</pubDate><dc:creator>timothyawiseman</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]timothyawiseman (12/5/2008)[/b][hr]To slightly modify a quote from Larry Wall, "[i]SQL[/i] is designed to give you several ways to do anything, so consider picking the most readable one."[/quote]I'm [i]definitely [/i]for that and glad to see anyone concerned about readability for a change... but even CTE's won't make readable code without a bit of concern for formatting and commenting.  Heh... I had one guy at my previous job that thought it was a good formatting choice to write each query on a single line and all lower case.</description><pubDate>Sat, 06 Dec 2008 08:18:37 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]G Bryant McClellan (12/1/2008)[/b][hr][quote][b]Jeff Moden (12/1/2008)[/b][hr]Good article... but, because of the title, I was actually expecting to see something about a problem or technique with CTE's that couldn't be done using other forms of code.  The article is really about how an index can help any query be it a CTE, Derived Table, View, etc.[/quote]Jeff,I was also thrown by the title although 'deception' gave me a clue. On the other hand, thanks to Kev for pointing out what should have been obvious...the use of indexes on columns commonly used in WHERE and ORDER BY clauses. I must admit that it gave me ideas on some inherited problem code that is built with CTE for no reason I can discern. The CTE is  (to me) no gain over standard SQL but potentially a gauze curtain covering up an underlying indexing issue. And I would venture that it was authored by a procedural programmer...[/quote]CTEs really bring two things tot he table that subqueries/derived tables do not.  One is the ability to use recursion.  It is definitely much maligned, and should be used with care, but it does have its uses.More significantly, it can improve readability.  It helps make it easier to read from top to bottom and it can move complexity from the "From" clause to a less cluttered area.  To slightly modify a quote from Larry Wall, "[i]SQL[/i] is designed to give you several ways to do anything, so consider picking the most readable one."</description><pubDate>Fri, 05 Dec 2008 17:50:30 GMT</pubDate><dc:creator>timothyawiseman</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>tada - http://www.sqlservercentral.com/Forums/Topic611416-338-1.aspx</description><pubDate>Wed, 03 Dec 2008 16:08:26 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Ah, was afraid you would ask that. I'll look for it.</description><pubDate>Wed, 03 Dec 2008 16:07:10 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Christopher Stobbs (12/2/2008)[/b][hr]Good point Steve, I was asking just yesterday about the left join vs NOT Exists, and well Gail gave me a very good visible answer and well ... like you say just cause it looks good doesn't mean it is good :-)[/quote]Can someone please post the link to the thread Steve and Chris was talking about?  Thanks.</description><pubDate>Wed, 03 Dec 2008 09:37:07 GMT</pubDate><dc:creator>Paul DB</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Nice article ...</description><pubDate>Tue, 02 Dec 2008 04:15:30 GMT</pubDate><dc:creator>Anipaul</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Good point Steve, I was asking just yesterday about the left join vs NOT Exists, and well Gail gave me a very good visible answer and well ... like you say just cause it looks good doesn't mean it is good :-)</description><pubDate>Tue, 02 Dec 2008 04:10:38 GMT</pubDate><dc:creator>Christopher Stobbs</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>If it's worth beating to death on the forums, it means that th ecommunity needs to hear it... people wanting to hear what you have to say or needing to hear it will attend... you don't have to try to please everybody out there, it's not possible (God knows I tried that one!).</description><pubDate>Tue, 02 Dec 2008 04:03:21 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Ninja's_RGR'us (12/1/2008)[/b][hr][quote][b]Steve Jones - Editor (12/1/2008)[/b][hr][quote]Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D[/quote]I'd bet it's more often how not to do something than the other way around.Hopefully we'll get you up there at PASS next year doing some of the teaching.[/quote]I second... let me go muy my tickets now!!![/quote]Awesome compliment's guys... I'm humbled.  :blush:   Thanks.  I'd have to think of something I haven't already beat to death on the forums, though... heh... not much left.</description><pubDate>Mon, 01 Dec 2008 23:26:59 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Steve Jones - Editor (12/1/2008)[/b][hr][quote]Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D[/quote]I'd bet it's more often how not to do something than the other way around.Hopefully we'll get you up there at PASS next year doing some of the teaching.[/quote]I second... let me go muy my tickets now!!!</description><pubDate>Mon, 01 Dec 2008 13:23:04 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote]Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D[/quote]I'd bet it's more often how not to do something than the other way around.Hopefully we'll get you up there at PASS next year doing some of the teaching.</description><pubDate>Mon, 01 Dec 2008 13:02:38 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>I'm wary of recursive CTEs as well. And concerned that because something appears clearer, you use it instead of something better. Another thread had a discussion on a left join v exists and how one is clearer to read, but not necessarily better.</description><pubDate>Mon, 01 Dec 2008 13:01:23 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]G Bryant McClellan (12/1/2008)[/b][hr][quote][b]Jeff Moden (12/1/2008)[/b][hr]Good article... but, because of the title, I was actually expecting to see something about a problem or technique with CTE's that couldn't be done using other forms of code.  The article is really about how an index can help any query be it a CTE, Derived Table, View, etc.[/quote]Jeff,I was also thrown by the title although 'deception' gave me a clue. On the other hand, thanks to Kev for pointing out what should have been obvious...the use of indexes on columns commonly used in WHERE and ORDER BY clauses. I must admit that it gave me ideas on some inherited problem code that is built with CTE for no reason I can discern. The CTE is  (to me) no gain over standard SQL but potentially a gauze curtain covering up an underlying indexing issue. And I would venture that it was authored by a procedural programmer...[/quote]For me, the only gain that usually comes from a CTE (one or more) is that it makes the programming read a little better in a "Top Down" fashion instead of having to read "Bottom Up" with derived tables.  Some like the ability to use recurrsive CTE's, but I've found that those are as bad or worse than cursors.</description><pubDate>Mon, 01 Dec 2008 12:46:56 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Ninja's_RGR'us (12/1/2008)[/b][hr][quote][b]Steve Jones - Editor (12/1/2008)[/b][hr]Jeff, it's not often someone is going to teach you something about T-SQL.[/quote]Still rolling on the floor on that one... can't be more true than that!!![/quote]Heh... thanks for the vote of confidence, but I learn something new about T-SQL everyday even if it's how to NOT do something. :D</description><pubDate>Mon, 01 Dec 2008 12:43:23 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]kevriley (12/1/2008)[/b][hr]Jeff- sorry the title didn't meet expectations - I was just trying to make it more exciting![/quote]Absolutely no problem... like I said, good article and it does stress the importance of not overlooking something not so obvious to some.  The devil's in the details and your good article brought that to light.  Thanks again for taking the time to write it.</description><pubDate>Mon, 01 Dec 2008 12:42:00 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Hi all,thanks for all your comments, very encouraging!Jeff- sorry the title didn't meet expectations - I was just trying to make it more exciting!Roger - I take all your points on board too - it's always good to hear both sides of any argument.All of my SSC contributions, whether articles, QOTDs or scripts are borne out of real-life experiences, and I just hope that they help anyone, experienced or newbie, in this strange land called SQL Server that we all play in.Kev</description><pubDate>Mon, 01 Dec 2008 08:57:18 GMT</pubDate><dc:creator>kevriley</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Steve Jones - Editor (12/1/2008)[/b][hr]Jeff, it's not often someone is going to teach you something about T-SQL.[/quote]Still rolling on the floor on that one... can't be more true than that!!!</description><pubDate>Mon, 01 Dec 2008 08:12:57 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>I thought it was an interesting story about how easy it is to overlook something. It's good to be reminded regularly that indexes matter and that new features, like CTEs, don't necessarily change the way T-SQL fundamentally works.Jeff, it's not often someone is going to teach you something about T-SQL.</description><pubDate>Mon, 01 Dec 2008 07:58:57 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Good article.  Most devs (database or otherwise) don't understand enough about the query optimizer and how it works with indexes, and your article has a good example of that.</description><pubDate>Mon, 01 Dec 2008 07:10:18 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]Jeff Moden (12/1/2008)[/b][hr]Good article... but, because of the title, I was actually expecting to see something about a problem or technique with CTE's that couldn't be done using other forms of code.  The article is really about how an index can help any query be it a CTE, Derived Table, View, etc.[/quote]Jeff,I was also thrown by the title although 'deception' gave me a clue. On the other hand, thanks to Kev for pointing out what should have been obvious...the use of indexes on columns commonly used in WHERE and ORDER BY clauses. I must admit that it gave me ideas on some inherited problem code that is built with CTE for no reason I can discern. The CTE is  (to me) no gain over standard SQL but potentially a gauze curtain covering up an underlying indexing issue. And I would venture that it was authored by a procedural programmer...</description><pubDate>Mon, 01 Dec 2008 05:54:20 GMT</pubDate><dc:creator>G Bryant McClellan</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>[quote][b]katesl (12/1/2008)[/b][hr]The CTE is the way programmers get around not having database permission to create a view.  Application programmers and "architects" who think "who needs a DBA?" write queries from the point of view that the database is a box that holds the records of interest to them.  The point of view of the DBA is to create views that are generally useful in the business model.[/quote]Lets be careful how we define application programmers. I have been developing in SQL Server since version 4. I am acknowledged by my DBAs as having considerably more SQL application development and tuning experience than they do, primarily because my background before SQL Server was in a declarative language and because I program SQL applications daily. Be that as it may, I still consider myself an application developer, even though my language of choice is SQL rather than C#.</description><pubDate>Mon, 01 Dec 2008 05:54:09 GMT</pubDate><dc:creator>G Bryant McClellan</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Kevin, while well understanding that the client just wants it to work, I'm going to play devil's advocate on this and argue who's got the "procedural" mindset.If we were talking about a relational database (vs a SQL database), there would no distinction between getting the needed values from a CTE or a "table", or a "view".   In all cases, you're just asking for the variable with the values of your query.Procedural programmers actually don't expect to get the value assigned at "declaration", but they do expect variables to hold onto values.   This is reasonable.   More to the point, though, the programmer shouldn't have to think about the mechanism anyway.  The programmer shouldn't be aware of a difference between a CTE or a "table" or a "view" - that's the whole point to relational databases.   The vendors however have done their best to make it look like an ISAM system, and now the so-called "object oriented" crowd has demoted it to a "persistence store" - even to the point of undoing most of the advantages of relational databases.Solving your client's issue means dealing with the system as it exists, and you did that, and got them going. But I think you might be a little quick to attribute the flaw to "procedural thinking".  It's in there, of course - but SQL and the database vendors don't make it easy to approach this right.  That's just arguing from the other side - your points are valid and well taken and it's an interesting real-life article.roger reid</description><pubDate>Mon, 01 Dec 2008 05:49:27 GMT</pubDate><dc:creator>Roger L Reid</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Good article... but, because of the title, I was actually expecting to see something about a problem or technique with CTE's that couldn't be done using other forms of code.  The article is really about how an index can help any query be it a CTE, Derived Table, View, etc.</description><pubDate>Mon, 01 Dec 2008 05:40:23 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>The CTE is the way programmers get around not having database permission to create a view.  Application programmers and "architects" who think "who needs a DBA?" write queries from the point of view that the database is a box that holds the records of interest to them.  The point of view of the DBA is to create views that are generally useful in the business model.</description><pubDate>Mon, 01 Dec 2008 02:51:23 GMT</pubDate><dc:creator>katesl</dc:creator></item><item><title>Power and Deception of CTEs</title><link>http://www.sqlservercentral.com/Forums/Topic610853-1228-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/T-SQL/64797/"&gt;Power and Deception of CTEs&lt;/A&gt;[/B]</description><pubDate>Sun, 30 Nov 2008 04:41:55 GMT</pubDate><dc:creator>kevriley</dc:creator></item></channel></rss>