Update Massive Table in smaller Batches or Transactions

[cc lang=”sql”]

SET NOCOUNT ON

DECLARE @BatchSize INT, @BatchCount INT, @totalCount INT

SET @BatchSize = 10000
SET @BatchCount = 0

SET @totalCount = (SELECT COUNT(*) FROM states)
WHILE EXISTS(SELECT * FROM states where desc like ‘%US%’)
BEGIN

UPDATE TOP (@BatchSize) states
set desc = ‘United States of America’
where desc like ‘%US%’

SET @BatchCount = @BatchCount + @@ROWCOUNT
END

SET NOCOUNT OFF

[/cc]

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>