Sql Server batch update
Hi there,
I have a simple update statement , but that updates 68 million records , so could any one help me in updating a query in batch and fetch next set of records ( get 1000 records -> update -> then fetch next 1000 records and update ...so on )
Here is my update statement :
update cds
set [mailing_history] = cdst.[mailing_history],
[mailing_history_initial] = cdst.[mailing_history_initial],
[mailing_history_followup] = cdst.[mailing_history_followup]
from dbo.campaign_detail_static2 cds
inner join dbo.campaign_detail_static_temp cdst
on cdst.campaign_code_skey = cds.[campaign_code_skey]
and cdst.sequence_nr = cds.sequence_nr
Thank you
May 6th, 2011 2:17pm
You can use a cursor (but your DBA may not be happy):
DECLARE @Col1 int, @Col2 int
DECLARE cur CURSOR FOR SELECT Col1, Col2 FROM Table WHERE Col3 = 12
OPEN cur
FETCH NEXT FROM cur into @Col1, @Col2
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Table2
SET Col5 = @Col2
WHERE Col2 = @Col1
FETCH NEXT FROM cur into @Col1, @Col2
END
CLOSE cur
DEALLOCATE cur
Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
May 6th, 2011 2:34pm