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
Solved

sql query

Posted on 2016-11-07
7
47 Views
Last Modified: 2016-11-23
hi experts,

 how can i fetch data from two columns present in two different table and update a third table's two columns with that data???
 Thanks.
0
Comment
Question by:sam shah
7 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41876739
you need to tell us what's the column(s) to be linked with these first 2 tables.
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 41876752
In a very generic way:
UPDATE Table1
SET ColumnName = (SELECT Table2.ColumnName
                FROM Table2
                        INNER JOIN Table3 ON Table2.ID = Table3.ID)

Open in new window

If you give us more information we can give you a more precise answer.
0
 

Author Comment

by:sam shah
ID: 41876897
MERGE INTO table1 a
   USING table2 b
   ON (b.col2 = a.col1)
   WHEN MATCHED THEN
      UPDATE
         SET a.col3 = b.col4

"how can i use cursor for this instead of merge'??
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41876900
Do not use cursor they are costly.. Query will suffice your problem.
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 41876901
So where's the 3rd table in your example?
Also, MERGE means UPDATE when exists and INSERT when not.

This is different from your main question.
Please confirm what you really want.
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41876905
Try something like.. Also provide table info and few rows.

;WITH CTE AS
(
	   SELECT b.col4 , b.col3
	   FROM table1 a
	   INNER JOIN table2 b
	   ON b.col1 = a.col1
)
UPDATE c
SET c.col4 = d.col4
FROM table3 c
INNER JOIN CTE d ON d.col3 = c.col3

Open in new window

0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 41895763
Sam, is your issue solved?
If it is please close this question by accepting one or more comments as solution.
If it is not solved please tell us what's missing so we can help you further.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

789 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