Question

  Asked: Apr 23 2008   9:49 PM GMT
  Asked by: Rl


bulk update in SQL Server 2005


BULK UPDATE, SQL Server 2005

I need to updata Column A1, Column A2 data in table A with Column B1, Column B2 data in table B for over 100,000 rows, how to write a procedure to accomplish this in sql server 2005?

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



Assuming that you have a column with distict 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
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Database and SQL Server.

Looking for relevant Database Whitepapers? Visit the SearchOracle.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register

Mrdenny  |   Apr 24 2008  12:58AM GMT

Check out my SQL Server blog “SQL Server with Mr Denny” for more SQL Server information.