If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > T-SQL or ISO

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-17-11, 16:57
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
T-SQL or ISO

Hi chumps...I mean chaps

I saw something new today. Wondered if anyone else had seen it, and if it was T-SQL specific or ISO. BoL entry for SELECT does not suggest it is valid, but it is. Whaddaya reckon the result will be? (tested in SQL 2005 & 2008)
Code:
SELECT  name
      , (object_id)
      , (SELECT schema_id) 
      , (SELECT TOP 937 type_desc)
FROM    sys.objects
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #2 (permalink)  
Old 02-17-11, 17:33
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Having a Sub-SELECT in the column list of a SELECT statement is ISO standard. The sub-select must return exactly one row with one column (so I wonder how the TOP 937 works here)

A select without a FROM clause however is non-standard.

But for the nested (SELECT schema_id) I don't see any advantage (apart from making the statement harder to understand) as schema_id is probably a column from sys.objects.
Reply With Quote
  #3 (permalink)  
Old 02-18-11, 02:28
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Oh I'm not claiming an advantage. It looks like total nonsense. The columns are all from the source table. That isn't the exact query, just an example of the syntax
Reply With Quote
  #4 (permalink)  
Old 02-18-11, 05:08
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
I simplified it by converting it into
Code:
SELECT  name
      , object_id
      , schema_id
      , type_desc
FROM    sys.objects
without changing the result. It returns 92 records on my test system.
Quote:
Oh I'm not claiming an advantage.
Think further! You must have heard of The Obfuscated C and Perl Contests. Now we can have our own Obfuscated MS-SQL Contests ® (OMSC) too! Just think of all the advantages it will bring to be able to write code only at most 2 other people on Earth are able to understand. And then imagine the benefits of one of your developers in your team sharpening his/her OMSC®-skills on production software on a daily base!
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Last edited by Wim; 02-18-11 at 11:19.
Reply With Quote
  #5 (permalink)  
Old 02-18-11, 08:04
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Oh I have some doozies. I'll start a new thread at the weekend
Reply With Quote
  #6 (permalink)  
Old 02-18-11, 10:21
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
Quote:
Originally Posted by shammat View Post
The sub-select must return exactly one row with one column (so I wonder how the TOP 937 works here)
.
It works because there's only one row to return. It could be TOP {anyPositiveNumber}. That one threw me for a loop at first too.
__________________
oh yeah... documentation... I have heard of that.

*** What Do You Want In The MS Access Forum? ***
Reply With Quote
  #7 (permalink)  
Old 02-18-11, 11:23
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Code:
SELECT  name
      , (object_id)
      , (SELECT schema_id) 
      , (SELECT TOP (937) type_desc)
      , (SELECT TOP (-0) type_desc)
FROM    sys.objects
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #8 (permalink)  
Old 02-18-11, 11:26
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
Now where did I put that "facepalm" image macro...
__________________
oh yeah... documentation... I have heard of that.

*** What Do You Want In The MS Access Forum? ***
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On