• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 67
  • Last Modified:

sql query

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
sam shah
Asked:
sam shah
1 Solution
 
Ryan ChongCommented:
you need to tell us what's the column(s) to be linked with these first 2 tables.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
sam shahAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Pawan KumarDatabase ExpertCommented:
Do not use cursor they are costly.. Query will suffice your problem.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
Pawan KumarDatabase ExpertCommented:
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now