<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx</link><description>SQL Books Online is wrong (or at least, on that page). I say that because it lists the FROM clause without brackets, suggesting that it’s not optional. This post is going to look at a few situations where we leave out the FROM clause, showing that it’s</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx#34817</link><pubDate>Tue, 12 Apr 2011 03:31:48 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34817</guid><dc:creator>Brad Schulz</dc:creator><description>&lt;p&gt;Excellent argument for the UNION ALL approach! &amp;nbsp;I like it!&lt;/p&gt;
&lt;p&gt;(And thanks for the mention).&lt;/p&gt;
&lt;p&gt;--Brad&lt;/p&gt;</description></item><item><title>re: SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx#34836</link><pubDate>Tue, 12 Apr 2011 14:34:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34836</guid><dc:creator>Matt Velic</dc:creator><description>&lt;p&gt;Thanks for participating, Rob! I knew that FROM was optional, but I hadn't thought of using APPLY instead of UNPIVOT. In my defense, I've never had to UNPIVOT anything, but I'm happy to have found my new method for when I do!&lt;/p&gt;</description></item><item><title>re: SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx#34949</link><pubDate>Sun, 17 Apr 2011 15:16:19 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34949</guid><dc:creator>Joe Celko</dc:creator><description>&lt;p&gt;T-SQL overloads the SELECT keyuword; ANSI does not. &lt;/p&gt;
&lt;p&gt;The &amp;lt;select sttement&amp;gt; does require a &amp;lt;FROM clause&amp;gt;; read your ANSI/ISO Standards. &amp;nbsp;I am doing this off the top of my head. &lt;/p&gt;
&lt;p&gt;The &amp;lt;assign statement&amp;gt; in Standard SQL is&lt;/p&gt;
&lt;p&gt; SET &amp;lt;target row consructor&amp;gt; = &amp;lt;expression row constructor&amp;gt;;&lt;/p&gt;
&lt;p&gt;The &amp;lt;assign statement&amp;gt; in T-SQL dialect is&lt;/p&gt;
&lt;p&gt; [SET | SELECT] &amp;lt;scalar assignment&amp;gt; {, &amp;lt;scalar assignment&amp;gt;};&lt;/p&gt;
&lt;p&gt; &amp;nbsp;&amp;lt;scalar assignment&amp;gt; ::= &amp;lt;variable&amp;gt; = &amp;lt;expression&amp;gt;&lt;/p&gt;
&lt;p&gt;Whwre SET can take only one scalar assignment. And, as you pointed out, T-SQL is a nightmare of bad proprietary syntax. &lt;/p&gt;</description></item><item><title>re: SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx#34954</link><pubDate>Sun, 17 Apr 2011 21:40:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34954</guid><dc:creator>Rob Farley</dc:creator><description>&lt;p&gt;Hi Joe,&lt;/p&gt;
&lt;p&gt;Yes, I'm most definitely restricting myself to T-SQL here. Oracle was my first RDBMS, and I was fine with following its rules regarding SET and DUAL. Having moved to the T-SQL world, I discovered that ANSI was broken in other ways. Some I liked, some I didn't.&lt;/p&gt;
&lt;p&gt;As much as I would like T-SQL to be as ANSI-like as possible, I also try to welcome the differences.&lt;/p&gt;
&lt;p&gt;Rob&lt;/p&gt;
</description></item><item><title>re: SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx#34959</link><pubDate>Mon, 18 Apr 2011 02:04:42 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34959</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Hey Rob,&lt;/p&gt;
&lt;p&gt;I'm a fan of UNPIVOT myself. &amp;nbsp;The following produces a nicer query plan (to my mind anyway) than the APPLY syntax:&lt;/p&gt;
&lt;p&gt;WITH &amp;nbsp; &amp;nbsp;Src&lt;/p&gt;
&lt;p&gt;AS &amp;nbsp; &amp;nbsp; &amp;nbsp;(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SELECT &amp;nbsp;SalesOrderID, &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OrderDate, &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ShipDate, &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DueDate,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OrigShip = &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CASE &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN ShipDate &amp;gt; DATEADD(DAY, 7, OrderDate) &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;THEN DATEADD(DAY, 7, OrderDate) &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;END&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FROM &amp;nbsp; &amp;nbsp;Sales.SalesOrderHeader&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;)&lt;/p&gt;
&lt;p&gt;SELECT &amp;nbsp;D.SalesOrderID, &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D.DateType, &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D.[Date]&lt;/p&gt;
&lt;p&gt;FROM &amp;nbsp; &amp;nbsp;Src&lt;/p&gt;
&lt;p&gt;UNPIVOT (&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[Date] FOR DateType IN (OrderDate, ShipDate, DueDate, OrigShip)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;) AS D&lt;/p&gt;
&lt;p&gt;;&lt;/p&gt;
&lt;p&gt;What do you think?&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;
</description></item><item><title>re: SELECT and WHERE, but no FROM clause</title><link>http://sqlblog.com/blogs/rob_farley/archive/2011/04/12/select-and-where-but-no-from-clause.aspx#34963</link><pubDate>Mon, 18 Apr 2011 03:33:48 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:34963</guid><dc:creator>Rob Farley</dc:creator><description>&lt;p&gt;Hi Paul,&lt;/p&gt;
&lt;p&gt;I'm not convinced it is nicer.&lt;/p&gt;
&lt;p&gt;It's almost identical (but has an extra operator) to the version that uses VALUES. But when you compare it to the UNION ALL version, you see a few differences.&lt;/p&gt;
&lt;p&gt;The difference that I see as significant is that the Filter for OrigShip appears before the concatenation, but when using UNPIVOT, it puts the Filter at the end (with 125860 being checked, 94404 going through), instead of a start-up filter that would be being used only 31465 times.&lt;/p&gt;
&lt;p&gt;But my main point is regarding the flexibility. UNPIVOT feels so inflexible, whereas APPLY can be used in far more ways.&lt;/p&gt;
&lt;p&gt;Thanks for your comment though - I always appreciate your feedback on these things.&lt;/p&gt;
&lt;p&gt;Rob&lt;/p&gt;
</description></item></channel></rss>