Solved

Update records in 1 table with results from another

Posted on 2014-11-21
5
161 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
[X]
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
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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