SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Updating Records in chunks


Updating Records in chunks

Author
Message
sheetaljain22
sheetaljain22
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 15
Hi,

I am looking for an approach wherein I can update the records in chunk like if there are 1500 records in a table to be updated so I want to update them in a set of 100 records.

Regards,
Sheetal
Tom Brown
Tom  Brown
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3364 Visits: 1491
Is there a reason for this piecemeal update?

I can think of 2 methods that may be of use. You could use TOP, or a WHILE Loop.
sheetaljain22
sheetaljain22
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 15
yes ...since I have got a batch job running every day and the records that are getting affected are in lakhs so need to implement a logic in part updation so that the system is not down for a long time ...

Do you have any sample logic ?
Tom Brown
Tom  Brown
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3364 Visits: 1491
something like this- assuming there is a 1 to 1 relationship tblA.pKey = tblB.updateKey

UPDATE tblA
SET tblA.col1 = tblB.col1
FROM tblB
WHERE tblA.pKey IN (SELECT TOP (100) tblB.updateKey
WHERE ... update not applied condition ...)

OR

DECLARE @COUNT INT
SET @COUNT = 0

WHILE (@COUNT < 1500 )
BEGIN

WITH NumberedRows AS (
SELECT ROW_NUMBER() OVER(Order By tblB.updateKey) AS RN, *
FROM tblB )
UPDATE tblA
SET tblA.col1 = NumberedRows.col1
FROM NumberedRows
WHERE NumberedRows.RN BETWEEN ( @COUNT AND (@COUNT + 100) )
AND tblA.pKey = tblB.updateKey
SET @COUNT = @COUNT + 100;

END
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search