Solved

sql query

Posted on 2016-11-07
7
34 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 49

Expert Comment

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

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Do not use cursor they are costly.. Query will suffice your problem.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Restore Script - Syntax Error 8 70
How to find duplicates in SQL Server 3 21
SQL Server memory Issue 7 73
Sql query 34 16
In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

743 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

8 Experts available now in Live!

Get 1:1 Help Now