Solved

UPDATE SQL for Multiple Records

Posted on 2011-09-22
9
337 Views
Last Modified: 2012-05-12
Hi,

We know how to use SQL UPDATE commands (e.g. UPDATE SET Field='Value WHERE etc.)

However we are wanting to UPDATE many records on mass

e.g. UPDATE a table of multiple records and a unique primary key with values from another table with the same common unique primary key in several records

Is this at all possible, and if so can you post some code that work, ohterwise the only other option is to scan thru every record in Table A, get it's Primary Key and then update from the 2nd table. When we have 10,000 records that'll prove a bit typesome!!
0
Comment
Question by:StuartK
[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
9 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 167 total points
ID: 36583475
If TableA and TableB both have ID as a shared Key,

UPDATE TableA SET Field1 = Tableb.Field1, Field2 = TableB.Field2
FROM TableA
INNER JOIN TableB ON TableA.ID = TableB.ID
0
 
LVL 29

Assisted Solution

by:QPR
QPR earned 167 total points
ID: 36583476
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 166 total points
ID: 36583485
Something like this:

UPDATE tableA
SET ColumnX = b.ColumnY
FROM tableA a INNER JOIN
    tableB b ON a.ID = b.ID

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36583488
slow fingers :)
0
 

Author Comment

by:StuartK
ID: 36587025
Three minutes later get a reply from three gurus!. What can I say!!! and the most perfect of answers from you all! Thanks to everyone. Only thing I can do is split between you.

Thank u so much guys!
0
 

Author Closing Comment

by:StuartK
ID: 36587048
Thanks again!
0
 

Author Comment

by:StuartK
ID: 36588998
Can I ask one further question related to this. i know I've allocated points already. I'd be so grateful for your guidance! Been trying to figure out!

We want our TableB to be from a select statement. Is there anyway we can embed this SELECT statement  below in the above statement

SELECT     MAX(RenewalDate) AS MinRenewalDate, AlarmRef
FROM         dbo.Tbl_AlarmServiceAgreements
GROUP BY AlarmRef

This would save us the hassle of having to create a Temproary Table and using that table as TableB above.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36590688
UPDATE  a
SET     Col1 = b.MinRenewalDate
FROM    TableA a
        INNER JOIN (SELECT  MAX(RenewalDate) MinRenewalDate,
                            AlarmRef
                    FROM    dbo.Tbl_AlarmServiceAgreements
                    GROUP BY AlarmRef
                   ) b ON a.ID = b.AlarmRef
0
 

Author Comment

by:StuartK
ID: 36590798
acperkins: you a guru man!!

Was just asking the guys who posted previous and unfortunately I'd already allocated points, but seriously I thank you so much. Again that backs up my opinion of gurus on EE. They don't do it for gain or points, just to help helpless people like myself in distress! lol. Thank you so much!
0

Featured Post

Technology Partners: 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

In this article I will describe the Backup & Restore 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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