February 22, 2014 at 8:02 pm
Comments posted to this topic are about the item Columns in Sub Queries
February 23, 2014 at 2:42 am
Didn't know that, :^)
Thanks, interesting question anyway.
Igor Micev,My blog: www.igormicev.com
February 23, 2014 at 5:03 pm
Interesting question - thanks
Hope this helps...
Ford Fairlane
Rock and Roll Detective
February 23, 2014 at 10:54 pm
Good one!
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
February 23, 2014 at 10:55 pm
Ford Fairlane (2/23/2014)
Interesting question - thanks
February 23, 2014 at 11:51 pm
nice question ... thanks for sharing
February 24, 2014 at 12:26 am
This was removed by the editor as SPAM
February 24, 2014 at 1:46 am
Great question, thanks.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
February 24, 2014 at 3:44 am
Pretty sure I only knew this one because a similar question has been posted before, so I suppose that means I'm learning something... 🙂
February 24, 2014 at 4:00 am
It's a common gotcha, always good to be reminded of it!
One of the reasons I always always use aliases 🙂
February 24, 2014 at 4:40 am
So I understood the intent of the question and got it correct, but bear in mind that on a case-sensitive collation you'd get the "Invalid column name 'CategoryId'." error.
February 24, 2014 at 5:22 am
Good questions. I've seen this gotcha rear its ugly head several times. It makes sense once you find it, but finding it is the hard part.
February 24, 2014 at 6:25 am
OK. I got it wrong, and now I know why I got it wrong. What I cannot understand is why it behaves like that in the first place! Surely if I say select 'this' from 'that', and 'that' does not have a 'this' it should generate an error.
Has anybody got a good explanation for why it does what it does, and where that would make sense in everyday use? Please?
February 24, 2014 at 6:39 am
Bob Cullen-434885 (2/24/2014)
OK. I got it wrong, and now I know why I got it wrong. What I cannot understand is why it behaves like that in the first place! Surely if I say select 'this' from 'that', and 'that' does not have a 'this' it should generate an error.Has anybody got a good explanation for why it does what it does, and where that would make sense in everyday use? Please?
It tries to match the column to the table and cannot do so because the table isn't specified with a table name or alias. The subquery has access to the parent query, so it tries to match the column there and succeeds. I personally would not use this "in everyday use", but that's how it works. Maybe someone better than I would use it, but I use aliases to explicitly specify where columns get pulled from. It catches me if I make a mistake before it even runs.
February 24, 2014 at 8:25 am
Bob Cullen-434885 (2/24/2014)
OK. I got it wrong, and now I know why I got it wrong. What I cannot understand is why it behaves like that in the first place! Surely if I say select 'this' from 'that', and 'that' does not have a 'this' it should generate an error.Has anybody got a good explanation for why it does what it does, and where that would make sense in everyday use? Please?
I've changed the DDL slightly to illustrate the point: -
IF object_id('tempdb..#category') IS NOT NULL
BEGIN;
DROP TABLE #category;
END;
IF object_id('tempdb..#product') IS NOT NULL
BEGIN;
DROP TABLE #product;
END;
SELECT ID, CategoryName
INTO #category
FROM (VALUES(1,'Cutlary'),(2,'Glassware'))a(ID, CategoryName);
SELECT ID, ProductName, CategoryID
INTO #product
FROM (VALUES(1,'Knife',1),(2,'Fork',1),(3,'Spoon',1),(4,'Tumbler',2),(5,'Pint Glass',3))a(ID, ProductName, CategoryID);
Now, imagine that instead of the query that the QOTD is showing, we were doing a JOIN.
SELECT CategoryName, ProductName
FROM #category cat
INNER JOIN #product prod ON cat.ID = prod.CategoryID;
You'd expect SQL Server to know where each of those columns in the SELECT statement come from, despite the lack of alias', due to logical query processing order, correct? Why would you think a sub-query in a WHERE clause would act any differently?
It's simply one of the many reasons why good alias' should be used whenever possible.
Viewing 15 posts - 1 through 15 (of 27 total)
You must be logged in to reply to this topic. Login to reply