Bulk update in SQL Server 2005

10 pts.
Tags:
BULK UPDATE
SQL Server 2005
I need to update Column A1, Column A2 data in table A with Column B1, Column B2 data in table B for over 100,000 rows. How do I write a procedure to accomplish this in SQL Server 2005?
ASKED: April 23, 2008  9:49 PM
UPDATED: October 18, 2013  6:51 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Assuming that you have a column with distinct values to show you which rows are which between the two tables this can be done with a simple update statement.

UPDATE TableA
SET TableA.A1 = TableB.B1,
TableA.A2 = TableB.B2
FROM TableB
WHERE TableA.A3 = TableA.B3

If you are worried about creating one massive transaction you can batch the operation into smaller chunks. This is done via the TOP keyword.

UPDATE TOP (1000) TableA
SET TableA.A1 = TableB.B1,
TableA.A2 = TableB.B2
FROM TableB
WHERE TableA.A3 = TableA.B3
AND TableA.A1 <> TableB.B1
AND TableA.A2 <> TableB.B2

You can put that into a loop like so.

DECLARE @d BIT
SET @d = 1

WHILE @d = 1
BEGIN
UPDATE TOP (1000) TableA
SET TableA.A1 = TableB.B1,
TableA.A2 = TableB.B2
FROM TableB
WHERE TableA.A3 = TableA.B3
AND TableA.A1 <> TableB.B1
AND TableA.A2 <> TableB.B2

IF @@ROWCOUNT = 0
@d = 0
END

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following