Solved

MSSQL 2000 Update column (multiple records) based on select from another table

Posted on 2009-03-30
3
494 Views
Last Modified: 2013-11-15
Ok as dumb as this might sound, I forgot how to update multiple records in a single table based on a select from another table.

For example I have a table I backed up and made changes to a live table and therefor they are the same structure and lets say I made a mistake and would like to revert back the changed data prior (assuming I did not do a begin tran), I would like perform an update and compare the id number to be the same in the live table as the backed up table. I know this code I am writing below is not a working peice of work but it was something i thought in my head to get started with.


UPDATE mytable

SET mycol = (select mycol from myothertable where active = 'y' and currentrevision = 'y')

where mytable.myid = myothertable.myid

Open in new window

0
Comment
Question by:smyers051972
3 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 300 total points
Comment Utility
You can do it two ways:

UPDATE mytable
SET mycol = (select mycol from myothertable where active = 'y' and currentrevision = 'y' and mytable.myid = myothertable.myid)

or

UPDATE mt
SET mycol = mot.mycol
from mytable mt
join myothertable mot
on mytable.myid = myothertable.myid
where mot.active = 'y'
and mot.currentrevision = 'y'
0
 
LVL 15

Assisted Solution

by:MNelson831
MNelson831 earned 200 total points
Comment Utility
Update MyTable
Set MyField = OtherTable.MyField
From MyTable inner join OtherTable on MyTable.UniqueID = OtherTable.UniqueID
0
 
LVL 1

Author Closing Comment

by:smyers051972
Comment Utility
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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

15 Experts available now in Live!

Get 1:1 Help Now