![]() |
Ambiguous column name 'voltage'
I'm fairly new to SQL and I ran into an Ambiguous column name error. Here's the error I received on my AJAX request:
Code:
<font face="Arial" size=2>Code:
whereStrSql=" left join excludedproducts E on (E.storeid=" & Session("StoreID") & " and E.id=P2.id) " |
Show us what the FULL SQL query/queries looks like.
Put in this line just AFTER your "...order by voltag2" line: Code:
Response.Write "<hr>whereStrSql: " & whereStrSql & "<hr>strSql: " & strSql & "<hr>" & vbNewLine |
From what was provided the query should look like this
Code:
SELECT P2.voltage,V.voltage voltage2,count(*) AS KOL Code:
FROM Lights_America..temp_products P2Code:
LEFT JOIN voltage V ON (P2.[voltage]=V.id)Code:
(P2.id=V.id) |
Here's the full SQL sorry about that:
Code:
select P2.voltage,V.voltage voltage2,count(*) as KOL from Lights_America..temp_products P2 left join voltage V on (P2.voltage=V.id) left join excludedproducts E on (E.storeid=9 and E.id=P2.id) where P2.id>0 and voltage in (1) and manufacturer_name in (143,135,153,171,151,217,184,185,181,179,183,175,150,123,130,129,121,128,18,14,66,37,77,65,11,68,46,60,43,41,45,108,38,98,40,26,48,25,28,42,62,13,9,30,39,70,47,64,74,116,33,97,12,107,101,16,29,57,59,7,15,71,1,72,54,10,19,51,53,35,69,114,118,17,67,52,63,34,102,76,31,191,188,190,133,134,194,196,160,159,163,209,216,213,214,211,139,58,208,204,168,166,142,165,164,138,140) and isnull(E.exc,0)<1 group by P2.voltage,V.voltage order by voltage2 |
Code:
select P2.voltage,V.voltage voltage2,count(*) as KOL -- missing a comma between V.voltage, voltage2 and an alias identifier ie P2.voltage2 or V.voltage2 |
If it's MS-SQL, it should be V.voltage as voltage2
If it's Oracle, it is correct (no "as"). Not sure about MySQL. |
Quote:
@OP: What DB are you using? SQL2008R2? SQLEXPRESS? MySQL? What management tool are you using? I think you need to take your query and get it working in your management tool. You design the queries first, then incorporate them into code... not find and error and then try to figure out if it is ASP or SQL or 'other' error; guarantee your SQL then worry about debugging ASP code. |
Quote:
|
ok- so are you using Microsoft SQL Server Management Studio? Copy the query you posted to a new query and try it- does it work? Why does it fail (if it does)?
|
First of all, SQL Server accepts *EITHER*
Code:
V.voltage AS voltage2And the "voltage" that is ambiguous is the one in red below: Code:
select P2.voltage,V.voltage AS voltage2,count(*) as KOL AND isnull(E.exc,0) < 1 will effectively convert the LEFT JOIN on E into an INNER JOIN. See here: http://www.codingforums.com/showthre...192#post818192 Possibly true of AND manufacturer_name in (143,135,...,140) as well. Can't tell, since the query doesn't say what table that field is coming from. Oh...and the two period in Lights_America..temp_products *ARE* legal. Normally, in SQL Server, you'd expect to see Lights_America.dbo.temp_products but I believe that when you omit the "owner" [that's what dbo is] (that is, have two periods) it means accept any owner. |
Quote:
Code:
Msg 208, Level 16, State 1, Line 1Code:
select P2.voltage,V.voltage voltage2,count(*) as KOL |
ah- did not know you could omit the "as" or the between ..'s ... I still though stand by my statement that should be tested in SQL query- it would have said which line the problem was instead of guessing games :eek:
|
dla: I *SHOWED* you the error. Look at the word voltage in red in my last post. SQL Server does NOT KNOW which "voltage" you mean by that one. Put the table alias in front of it.
But ALSO read what I wrote about LEFT JOIN being converted to INNER JOIN by your other mistake. |
voltage should be P2.voltage, I _think_.
|
I *THINK* that this fixes both problems. But of course I can't be sure:
Code:
select P2.voltage,V.voltage AS voltage2,count(*) as KOL |
| All times are GMT +1. The time now is 05:45 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.