Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« Use Filters in SSMS to only see the things that are of interest to youRead from your mirror in SQL Server »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Here is something interesting to think about

Create this table and insert these 2 rows

  1. CREATE TABLE TableName(id INT, name VARCHAR(50))
  2. INSERT TableName VALUES(1,'bla')
  3. INSERT TableName VALUES(2,'bla2')

Now if you try to do something like this

  1. SELECT TOP 1 ID,Name
  2.    FROM TableName
  3.    ORDER BY Name
  4.    UNION ALL
  5.    SELECT 0,''

You will get the following error

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'UNION'.

Interestingly, this same code will work if you use it in the following matter

  1. SELECT TOP 1 *
  2. FROM (
  3.    SELECT TOP 1 ID,Name
  4.    FROM TableName
  5.    ORDER BY Name
  6.    UNION ALL
  7.    SELECT 0,''
  8. ) X
  9. ORDER BY ID DESC

This same code also works with Common Table Expressions

  1. WITH query AS (
  2.     SELECT TOP 1 ID,Name
  3.    FROM TableName
  4.    ORDER BY Name
  5.    UNION ALL
  6.    SELECT 0,'')
  7.  
  8.  
  9.   SELECT TOP 1
  10.          x.id,
  11.          x.name
  12.     FROM query x
  13. ORDER BY x.id DESC

My assumption is that the optimizer disregards any order by in the subquery. Still I find this interesting because usually a query will work, you stick it inside another piece of code and it will complain about something




*** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
336 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

2 comments

Comment from: John Sheehan [Visitor] · http://twitter.com/johnsheehan
I was just working on something similar today and your original snippet would work if changed to this:

SELECT TOP 1 ID,Name
FROM TableName
UNION ALL
SELECT 0 ID,'' Name
ORDER BY Name
02/06/10 @ 12:13
Comment from: SQLDenis [Member] Email
John, I know that what you have works. What I am trying to figure out is why something that doesn't (and shouldn't work) does work in a CTE or subquery
02/06/10 @ 12:30

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)