dbTalk Databases Forums  

SQL 2005 Ambiguous column name

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL 2005 Ambiguous column name in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rdraider
 
Posts: n/a

Default SQL 2005 Ambiguous column name - 10-02-2007 , 03:12 PM






I have a strange issue and was hoping somebody could explain. The below
query gives an ambiquous column name error on an install of SQL 2005
Standard, sp2. This same query works fine on another install of SQL 2000
Standard sp4 and SQL 2005 Workgroup. The order by clause is causing the
error. I know this is easily fixed, just wondering why the error only
occurs on a particular install of SQL 2005.

SELECT DISTINCT ProcessName, ProcessName
FROM ProcessLog
ORDER BY ProcessName


Thanks.



Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-02-2007 , 04:33 PM






rdraider (rdraider (AT) sbcglobal (DOT) net) writes:
Quote:
I have a strange issue and was hoping somebody could explain. The below
query gives an ambiquous column name error on an install of SQL 2005
Standard, sp2. This same query works fine on another install of SQL 2000
Standard sp4 and SQL 2005 Workgroup. The order by clause is causing the
error. I know this is easily fixed, just wondering why the error only
occurs on a particular install of SQL 2005.

SELECT DISTINCT ProcessName, ProcessName
FROM ProcessLog
ORDER BY ProcessName
You get an error because this is incorrect SQL. ORDER BY is special,
because this is the only place where you can refer to columns in the SELECT
list. But there are two ProcessName, which of them do you want to order by?
That may seem like a silly question but consider:

select top 20 a = CustomerID, a = OrderID
from Orders
order by a

In SQL 2000, this sorts by CustomerID which is obviously a bug. It should
give you the same error message as SQL 2005 does. The fact that is
accepted on SQL 2005 workgroup, I would assume is due to that you ran it
in a database with the compatibility level set set to 80 (= SQL 2000).

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
rdraider
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-02-2007 , 05:04 PM



You are correct about the compatibility level on SQL 2005 set to 80.
I know this is incorrect SQL but it comes from a web app. I went ahead and
edited the ASP page but will need to have the developer fix it.

Thanks.


"Erland Sommarskog" <esquel (AT) sommarskog (DOT) se> wrote

Quote:
rdraider (rdraider (AT) sbcglobal (DOT) net) writes:
I have a strange issue and was hoping somebody could explain. The below
query gives an ambiquous column name error on an install of SQL 2005
Standard, sp2. This same query works fine on another install of SQL 2000
Standard sp4 and SQL 2005 Workgroup. The order by clause is causing the
error. I know this is easily fixed, just wondering why the error only
occurs on a particular install of SQL 2005.

SELECT DISTINCT ProcessName, ProcessName
FROM ProcessLog
ORDER BY ProcessName

You get an error because this is incorrect SQL. ORDER BY is special,
because this is the only place where you can refer to columns in the
SELECT
list. But there are two ProcessName, which of them do you want to order
by?
That may seem like a silly question but consider:

select top 20 a = CustomerID, a = OrderID
from Orders
order by a

In SQL 2000, this sorts by CustomerID which is obviously a bug. It should
give you the same error message as SQL 2005 does. The fact that is
accepted on SQL 2005 workgroup, I would assume is due to that you ran it
in a database with the compatibility level set set to 80 (= SQL 2000).

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx



Reply With Quote
  #4  
Old   
steve
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-02-2007 , 09:28 PM



But why does this work?

SELECT CustomerID,CustomerID
FROM Orders

SELECT DISTINCT CustomerID,CustomerID
FROM Orders

This is what I was talking about in:

http://beyondsql.blogspot.com/2007/1...f-sql-cte.html

best,
steve
www.beyondsql.blogspot.com


Reply With Quote
  #5  
Old   
Serge Rielau
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-03-2007 , 08:58 AM



steve wrote:
Quote:
But why does this work?

SELECT CustomerID,CustomerID
FROM Orders

SELECT DISTINCT CustomerID,CustomerID
FROM Orders

This is what I was talking about in:

http://beyondsql.blogspot.com/2007/1...f-sql-cte.html

These are two completely separate topics.
Let's apply real life as a metaphor here:
There is no rule against (in fact it is quite likely that) you have two
kids with the same first name in class (Say: "William").
A problem only arises when you try to reference them (e.g. in the ORDER
BY clause or an outer query).
Then you better give them distinct names "Bill", "Billy", "Will", ...

The intent of a CTE is that it is referenced. The same isn't necessarily
true for columns in the select list (positional bind-out of a cursor)

ORDER BY is even more interesting here since it has to resolve against
two scopes: The "exposed" column names in the select list as well as the
columns accessible through the FROM clause. AFAIK the closer scope for
ORDER BY is the select list. So it's within the perogative of the DBMS
DBMS to raise an error here.
Things get more clearer when we enhance the example a bit:
SELECT customerid AS X, customerid * -1 AS X FROM orders ORDER BY X

The DISTINCT keyword (and GROUP BY while we're at it) has absolutely
NOTHING to do with ORDER BY.
There are plenty of other ways to de-dup a set than ORDERering. E.g.
hashing, or reliance on partitioning of various flavours.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab


Reply With Quote
  #6  
Old   
steve
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-03-2007 , 02:50 PM



On Oct 3, 6:58 am, Serge Rielau <srie... (AT) ca (DOT) ibm.com> wrote:
Quote:
The intent of a CTE is that it is referenced. The same isn't necessarily
true for columns in the select list (positional bind-out of a cursor)
Hi,

So your explaining another aspect of 'referential' integrity. :-)
I reference it (CTE,Order By etc) the integrity is checked and
enforced.
And if I don't reference it it's my tough luck or my oversight and I
could wind up with a big mess?
Interestingly, it does resemble the logic of a FK reference in a
twisted way. But here chance seems to play a major part :-)
These queries go happly on their merry way:

SELECT a.orderid,a.customerid as Cust,a.shipcountry as
Cust,b.productid
FROM orders as a join [order details] as b
on a.orderid=b.orderid

SELECT a.orderid,a.orderid,a.customerid as Cust,a.shipcountry as
Cust,b.productid
FROM orders as a join [order details] as b
on a.orderid=b.orderid

best,
steve

www.beyondsql.blogspot.com



Reply With Quote
  #7  
Old   
Serge Rielau
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-03-2007 , 05:06 PM



steve wrote:
Quote:
On Oct 3, 6:58 am, Serge Rielau <srie... (AT) ca (DOT) ibm.com> wrote:
The intent of a CTE is that it is referenced. The same isn't necessarily
true for columns in the select list (positional bind-out of a cursor)

Hi,

So your explaining another aspect of 'referential' integrity. :-)
I reference it (CTE,Order By etc) the integrity is checked and
enforced.
And if I don't reference it it's my tough luck or my oversight and I
could wind up with a big mess?
Not at all.
I presume you are aware that columns in the select list can actually be
unnamed:
SELECT c1 * c2, foo(c3) FROM T
You now have two unnamed columns returned from the select list.
That's all right, chances are your .NET variables that you're going to
bind them out to have different names anyway and your bind-out goes by
position anyway.
Obviously when you create a view:
CREATE VIEW v AS SELECT c1 * c2, foo(c3) FROM T
that view would be quite useless because you can reference the columns
So you give them names either in the view signature or by naming each
expression.

Quote:
Interestingly, it does resemble the logic of a FK reference in a
twisted way. But here chance seems to play a major part :-)
These queries go happly on their merry way:

SELECT a.orderid,a.customerid as Cust,a.shipcountry as
Cust,b.productid
FROM orders as a join [order details] as b
on a.orderid=b.orderid

SELECT a.orderid,a.orderid,a.customerid as Cust,a.shipcountry as
Cust,b.productid
FROM orders as a join [order details] as b
on a.orderid=b.orderid
And why shouldn't they?

This has nothing to do with luck. It's how the SQL Standard was
consciously designed. Now if you want to enforce that all expressions in
the select list must be named and be unambiguously feel free to write
your own standard. I sure am too lazy to label stuff I don't need :-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab


Reply With Quote
  #8  
Old   
steve
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-04-2007 , 01:25 AM



Hello Serge,

Your a good sport, have a sense of humor, smart and can write.
How did you manage to stay in IT?

Lets put the CTE to bed, I was only using it to make some
much broader points. The things works as advertised

I have been annoying people about Dataphor (www.alphora.com).
A product based on Date's outline of a future system (TTM.).
Unfortunately the concept of a table 'type' and 'variable'
is not something most ms sql users (who think net is for fishing)
are warming up too It uses an sql database as a respository.
I've used it with sql server but look forward to using it
with DB2 (yes you can use pass-thru queries as the result goes
thru the cli and is treated like any other table (variable).
I'm sure your plate is full but if you find some spare time
perhaps browse the site/help docs and if you download the beast
that would great. I blog about it (illustrated with relatively
simple examples to introduce the ideas) @
www.beyondsql.blogspot.com

The gentler idea is to use something like D4 as a complement
not a complete replacement for sql. Hence the emphasis on
application development where we're not necessarily updating
2 million rows

While I have no business connection to Alphora, I have a big
intellectual one with the general idea. Shesh, I'd like more company.
I'd love to see some impressions from a professional of your stature
(yeah somewhat brownoseish but said with sincerity

Thanks and best,
steve dassin
www.rac4sql.net
www.beyondsql.blogspot.com


Reply With Quote
  #9  
Old   
Serge Rielau
 
Posts: n/a

Default Re: SQL 2005 Ambiguous column name - 10-05-2007 , 02:30 PM



steve wrote:
Quote:
Hello Serge,

Your a good sport, have a sense of humor, smart and can write.
How did you manage to stay in IT?
They say the ultimate expert knows everything about nothing.
I guess I'm unemployable for anything else...

Quote:
Lets put the CTE to bed, I was only using it to make some
much broader points. The things works as advertised

I have been annoying people about Dataphor (www.alphora.com).
A product based on Date's outline of a future system (TTM.).
Unfortunately the concept of a table 'type' and 'variable'
is not something most ms sql users (who think net is for fishing)
are warming up too It uses an sql database as a respository.
I've used it with sql server but look forward to using it
with DB2 (yes you can use pass-thru queries as the result goes
thru the cli and is treated like any other table (variable).
I'm sure your plate is full but if you find some spare time
perhaps browse the site/help docs and if you download the beast
that would great. I blog about it (illustrated with relatively
simple examples to introduce the ideas) @
www.beyondsql.blogspot.com

The gentler idea is to use something like D4 as a complement
not a complete replacement for sql. Hence the emphasis on
application development where we're not necessarily updating
2 million rows
I'll take a look. The job posting on the site is cute....
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.