![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
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) |
#7
| |||
| |||
|
|
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. |
|
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? |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
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. |
|
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.... |
![]() |
| Thread Tools | |
| Display Modes | |
| |