SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UPDATE with multiple joins


UPDATE with multiple joins

Author
Message
roy.tollison
roy.tollison
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 200
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25845 Visits: 17509
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 Modens 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)
roy.tollison
roy.tollison
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 200
that's ok, i tinkered with it some more and managed to get what i needed.

Thank you,
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27968 Visits: 39922
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

roy.tollison
roy.tollison
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 200
That is what i settled on.

Thank you.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search