Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SubQuery


SubQuery

Author
Message
rfr.ferrari
rfr.ferrari
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1351 Visits: 13619
good question!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
the period you fastest growing is the most difficult period of your life!

Ken Wymore
Ken Wymore
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4418 Visits: 2342
SQLRNNR (9/19/2011)
Kenneth Wymore (9/19/2011)
Any idea as to why they would allow this behavior in a join? Seems to me like it would actually introduce more confusion than convenience.


I think they have to allow it due to the requirement that a subquery that is based on values instead of a query requires the same syntax.

Here's an article on that. http://jasonbrimhall.info/2011/08/31/bitwise-and-derived-table-revisited/


I reviewed your other post about this and I see what you mean about the subquery using values instead of a table select. I have never seen values used in that way exactly but I am sure there are times when it is necessary. When there is a list of static values to reference, I have usually seen it coded as follows.


SELECT *
FROM (
   SELECT 1 as a, 2 as b
   UNION ALL
   SELECT 3 as a, 4 as b
   UNION ALL      
   SELECT 5 as a, 6 as b            
   UNION ALL      
   SELECT 7 as a, 8 as b      
   UNION ALL
   SELECT 9 as a, 10 as b      
) as MyTable;

--OR using a temp table

IF OBJECT_ID(N'TempDB..#MyTable') IS NOT NULL
BEGIN
DROP TABLE #MyTable
END

CREATE TABLE #MyTable
(a INT, b INT)

INSERT INTO #MyTable

SELECT 1 as a, 2 as b
UNION ALL
SELECT 3 as a, 4 as b
UNION ALL      
SELECT 5 as a, 6 as b            
UNION ALL      
SELECT 7 as a, 8 as b      
UNION ALL
SELECT 9 as a, 10 as b               
;

SELECT * FROM #MyTable;   


Using the union all statements is a bit tedious but that is what I have normally seen. If the same set needs to be used differently for multiple queries then it is typically dropped into a temp table or a regular table. I have seen this option used before just to keep the main query from looking overly complicated too.

I am guessing that using a set of values like you showed on your post would be more common when dealing with applications? For example, where you don't want to insert user supplied values into a table but instead are just using them temporarily in the subquery?
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21067 Visits: 18258
Kenneth Wymore (9/20/2011)
SQLRNNR (9/19/2011)
Kenneth Wymore (9/19/2011)
Any idea as to why they would allow this behavior in a join? Seems to me like it would actually introduce more confusion than convenience.


I think they have to allow it due to the requirement that a subquery that is based on values instead of a query requires the same syntax.

Here's an article on that. http://jasonbrimhall.info/2011/08/31/bitwise-and-derived-table-revisited/


I reviewed your other post about this and I see what you mean about the subquery using values instead of a table select. I have never seen values used in that way exactly but I am sure there are times when it is necessary. When there is a list of static values to reference, I have usually seen it coded as follows.


SELECT *
FROM (
   SELECT 1 as a, 2 as b
   UNION ALL
   SELECT 3 as a, 4 as b
   UNION ALL      
   SELECT 5 as a, 6 as b            
   UNION ALL      
   SELECT 7 as a, 8 as b      
   UNION ALL
   SELECT 9 as a, 10 as b      
) as MyTable;

--OR using a temp table

IF OBJECT_ID(N'TempDB..#MyTable') IS NOT NULL
BEGIN
DROP TABLE #MyTable
END

CREATE TABLE #MyTable
(a INT, b INT)

INSERT INTO #MyTable

SELECT 1 as a, 2 as b
UNION ALL
SELECT 3 as a, 4 as b
UNION ALL      
SELECT 5 as a, 6 as b            
UNION ALL      
SELECT 7 as a, 8 as b      
UNION ALL
SELECT 9 as a, 10 as b               
;

SELECT * FROM #MyTable;   


Using the union all statements is a bit tedious but that is what I have normally seen. If the same set needs to be used differently for multiple queries then it is typically dropped into a temp table or a regular table. I have seen this option used before just to keep the main query from looking overly complicated too.

I am guessing that using a set of values like you showed on your post would be more common when dealing with applications? For example, where you don't want to insert user supplied values into a table but instead are just using them temporarily in the subquery?


That is one place. It is not a very common thing to see - imo. I've seen it in solutions here at SSC. I have also used that method on occasion for that very reason (and because it is faster).



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2009 Visits: 1189
Thanks for Valuable Question.Keep Posting

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Rich Mechaber
Rich Mechaber
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1087 Visits: 3661
Well, I got it wrong [sic] for the right [sic] reason: like the rest of you, if my column names don't match in my queries, I usually get errors. Usually. Not here, though, apparently.

Interesting syntax - thanks for the question.

I will now endeavor to forget I ever saw this, or I'll start getting sloppy and be back to column name mismatch errors! :-D

Rich
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search