Solved

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

Posted on 2009-03-30
3
531 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
ID: 24020668
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
ID: 24020673
Update MyTable
Set MyField = OtherTable.MyField
From MyTable inner join OtherTable on MyTable.UniqueID = OtherTable.UniqueID
0
 
LVL 1

Author Closing Comment

by:smyers051972
ID: 31564413
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 51
simple bash script needed to unpack tar.gz 4 26
UPDATE JOIN multiple tables 5 19
Find special characters using tSQL 6 15
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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