I have 2 tables:
Example of Table1:
StationID1 StationID2 Name1 Name2 Lattitude1 Longitude1 Lattitude2 Longitude2 Distance
------------------------------------------------------------------------------------------------
93353477 52452 FOO BAR NULL NULL NULL NULL NULL
93353527 52452 HENRY BENNY NULL NULL NULL NULL NULL
93353551 52452 GALE SAM NULL NULL NULL NULL NULL
Example of Table2:
IDInfo Name Lattitude Longitude
-------------------------------------------
93353477 BAR 37.929654 -87.029622
I want to update this table with the coordinate information which resides in tableA
. I tried to do the following as per SQL Server 2005: The multi-part identifier … could not be bound
update table1
set t1.[Lattitude1] = t2.[Lattitude]
from table1 t1
left join table2 t2
on (t1.StationID1 = t2.IDInfo)
I get the following error message:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "t1.Lattitude1" could not be bound.
However, if I do the following it works which I can then store into another table.
SELECT t1.[StationID1]
,t1.[StationID2]
,t1.[Name1]
,t1.[Name2]
,t2.[Lattitude] AS [Lattitude1]
,t2.[Longitude] AS [Longitude1]
,t3.[Lattitude] AS [Lattitude2]
,t3.[Longitude] AS [Longitude2]
from table1 t1
left join table2 t2
on (t1.StationID1 = t2.IDInfo)
left join table2 t3
on (t1.StationID2 = t2.IDInfo)
I am very new to SQL and am having a difficult time understanding why some things work and others don't. Based on the link I posted above my initial query should have worked - no? Perhaps I'm not thinking straight as I have spent many hours trying this and I finally got help from a co-worker (she suggested the approach I mention above).
A multipart identifier is any description of a field or table that contains multiple parts - for instance MyTable. SomeRow - if it can't be bound that means there's something wrong with it - either you've got a simple typo, or a confusion between table and column.
The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.
I think you can modify your UPDATE statement to reference the table alias in the UPDATE line.
update t1
set t1.[Lattitude1] = t2.[Lattitude]
from table1 t1
left join table2 t2
on (t1.StationID1 = t2.IDInfo)
You need to change the inner table and give a different allias to the columns that are similar. This should work.
update table1
set [Lattitude1] = x.[lat]
from
(
SELECT IDInfo [id], Lattitude [lat] FROM
table2
) x
WHERE
StationID1 = x.[id]
In your particular case its not necessary to rename Lattitude to lat, but if you end up updating a table with itself and force yourself into giving the columns different names, it will save you headaches down the road.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With