Solved

MS SQL Multi Joins Update/Select

Posted on 2015-01-16
3
91 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Expert Comment

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

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 13

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

705 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