Solved

MS SQL Multi Joins Update/Select

Posted on 2015-01-16
3
88 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 48
SQL Convert rows to columns 5 31
VMware PVSCSI SQL Server 2016 AlwaysOn 2 33
How to build a logic for passwords according to initials? 13 39
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

737 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