Solved

Update records in 1 table with results from another

Posted on 2014-11-21
5
158 Views
Last Modified: 2014-11-21
I may not have explained what I am looking for well in the the title, but this is what I am trying to accomplish.

I have results from 1 query (see below), and I want to update all of the records in TableB that have the same ID as the results from the select statement. For example, if the query returns 3 IDs, I want to take those and update the records in TableB based on those IDs. (Update TableB... set somevalue='blah'... etc.

I know this can be done a few different ways in coldfusion or even just using the SQL. I just need a rough example.

Thanks for any help you can offer

SELECT     TableA.TheID, TableB.AValue, TableA.AUser, 
                      TableB.TheID
FROM         TableA INNER JOIN
TableB ON TableA.TheID = TableB.TheID

Open in new window

0
Comment
Question by:earwig75
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40457602
UPDATE TableB
SET TableB.AValue = TableA.AUser
FROM         TableA
INNER JOIN TableB ON TableA.TheID = TableB.TheID
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40457607
I would suggest testing it by surrounding it by:

BEGIN TRAN
Select * from TableB
<<Insert Code>>
Select * from TableB
ROLLBACK TRAN

This way you can test if the update has worked without committing the transaction.
0
 

Author Comment

by:earwig75
ID: 40457625
Thank you, I may not have explained myself. I don't want to set items to be equal between the tables.

If my select statement returns IDs: 5,7 and 9, I want to update the records in tableB where the ID is 5,7 and 9...

Something like this:

Update TableB
Set Status="Open", Color="Green"
Where TheID......(this is where i want to use the IDs from the select)

Thank you.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40457635
Roughly the same thing.

UPDATE TableB
SET TableB.Status="Open", TableB.Color="Green"
FROM         TableA
INNER JOIN TableB ON TableA.TheID = TableB.TheID
0
 

Author Closing Comment

by:earwig75
ID: 40457659
Awesome, that's what I was looking for. Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Delete Query 9 33
SQL Syntax 5 37
Connection to multiple databases 13 20
Problem when I run a simple storeproc - help 4 17
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

803 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