Solved

MS SQL Multi Joins Update/Select

Posted on 2015-01-16
3
76 Views
Last Modified: 2015-01-18
Hi,
I have 3 tables structured as follows
table1 (equipnum, field1, field2)  (this table is used for reporting
table2 (projectID, equipnum, field11, field21)
table3 (projectID, equipnum, field111, field211)

I want to update table1 with values from table2 and table3
The join key table1-table2 : equipnum, table2-table3: projectID, equipnum

Regards
0
Comment
Question by:Omer-Pitou
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40554884
0
 
LVL 14

Expert Comment

by:Vikas Garg
ID: 40554924
Hello,

You can try something like this

UPDATE Table1
SET Col2 = t2.Col2,
Col3 = t2.Col3
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.Col1 = t2.Col1
WHERE t1.Col1 IN (21, 31)
GO

Open in new window

0
 
LVL 12

Accepted Solution

by:
Nakul Vachhrajani earned 500 total points
ID: 40556436
@Vikas: Your solution will work, only catch is that using UPDATE Table1.... FROM Table1 t1 may cause the table to block itself in an environment that supports parallelism. A better alternate is to use UPDATE t1....FROM Table1 t1 as shown below:

UPDATE t1
SET t1.field1 = t2.field11 - field111,
    t1.field2 = t2.field21 - field211
FROM dbo.table1 AS t1
INNER JOIN dbo.table2 AS t2 ON t1.equipnum = t2.equipnum
INNER JOIN dbo.table3 AS t3 ON t2.projectID = t3.projectID
                           AND t2.equipnum = t3.equipnum;

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now