Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

UPDATE with multiple joins Expand / Collapse
Author
Message
Posted Friday, November 8, 2013 11:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, November 19, 2013 7:04 AM
Points: 72, Visits: 172
and I can't seem to find the correct syntax.
UPDATE UPDTABLE
SET updtbl_lat = i.infotbl_lat,
updtbl_long = i.infotbl_long
FROM INFOTABLE i inner join DTLTABLE d on d.dtltbl_group = i.infotbl_group
inner join DTLTABLE e on e.dtltbl_code = updtbl_code
WHERE updtbl_number = i.infotbl_number

I get error that updtbl_number in the WHERE clause is an invalid column name...

the relationship is kind of a round robin thing. The UPDTABLE ties into the DTLTABLE through the _code column then
the columns from the UPDTABLE.updtbl_number and DTLTABLE.dtltbl_group tie into the INFOTABLE.infotbl_group and INFOTABLE.infotbl_number. Then i need the INFOTABLE columns _lat and _long to update to the UPDTABLE columns.

UPDTABLE
updtbl_lat
updtbl_long
updtbl_number
updtbl_code

INFOTABLE
infotbl_lat
infotbl_long
infotbl_group
infotbl_number

DTLTABLE
dtltbl_code
dtltbl_group
Post #1512774
Posted Friday, November 8, 2013 12:03 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:16 PM
Points: 13,301, Visits: 12,159
roy.tollison (11/8/2013)
and I can't seem to find the correct syntax.
UPDATE UPDTABLE
SET updtbl_lat = i.infotbl_lat,
updtbl_long = i.infotbl_long
FROM INFOTABLE i inner join DTLTABLE d on d.dtltbl_group = i.infotbl_group
inner join DTLTABLE e on e.dtltbl_code = updtbl_code
WHERE updtbl_number = i.infotbl_number

I get error that updtbl_number in the WHERE clause is an invalid column name...

the relationship is kind of a round robin thing. The UPDTABLE ties into the DTLTABLE through the _code column then
the columns from the UPDTABLE.updtbl_number and DTLTABLE.dtltbl_group tie into the INFOTABLE.infotbl_group and INFOTABLE.infotbl_number. Then i need the INFOTABLE columns _lat and _long to update to the UPDTABLE columns.

UPDTABLE
updtbl_lat
updtbl_long
updtbl_number
updtbl_code

INFOTABLE
infotbl_lat
infotbl_long
infotbl_group
infotbl_number

DTLTABLE
dtltbl_code
dtltbl_group


Pretty sparse on details here. You are trying to update the table UPDTABLE but that table is not in the list of tables. Without more detail than what you have posted I can't even begin to offer a correct syntax.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1512787
Posted Friday, November 8, 2013 1:08 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, November 19, 2013 7:04 AM
Points: 72, Visits: 172
that's ok, i tinkered with it some more and managed to get what i needed.

Thank you,
Post #1512801
Posted Friday, November 8, 2013 1:22 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 12,910, Visits: 32,013
here's my best guess based on your post's info:

i'm explicitly aliasing the target table, so it might make it a little clearer:

UPDATE MyTarget 
SET MyTarget.updtbl_lat = i.infotbl_lat,
MyTarget.updtbl_long = i.infotbl_long
FROM UPDTABLE MyTarget
INNER JOIN INFOTABLE i
ON MyTarget.updtbl_number = i.infotbl_number
INNER JOIN DTLTABLE d
ON d.dtltbl_group = i.infotbl_group
INNER JOIN DTLTABLE e
ON e.dtltbl_code = MyTarget.updtbl_code






Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1512803
Posted Friday, November 8, 2013 1:24 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, November 19, 2013 7:04 AM
Points: 72, Visits: 172
That is what i settled on.

Thank you.
Post #1512804
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse