Solved

Update records in 1 table with results from another

Posted on 2014-11-21
5
160 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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