dbTalk Databases Forums  

T-SQL: Batch update

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss T-SQL: Batch update in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Karthik R
 
Posts: n/a

Default T-SQL: Batch update - 10-04-2004 , 01:31 PM






Hi,
I want to do run a very large UPDATE Statement covering millions of
records. I saw some material in the web that shows how to acheive this
using SET ROWCOUNT, Infinite WHILE LOOP and @@ROWCOUNT.

I am not getting the approach. How can i write a update statement to
UPDATE records batch by batch to minimize transaction logging using
T-SQL?

Any pointers will be of great help.

Thanks in advance

Reply With Quote
  #2  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: T-SQL: Batch update - 10-04-2004 , 01:47 PM






The idea is that after each batch there are less items to update and
therefore when @@rowcount gets to 0 then you are finished

Imagine we have a table

CREATE TABLE InABatch(col1 int)
GO
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)

We have five rows but i want to only update 2 rows at a time so i will have
to issue an UPDATE statement *4


SET ROWCOUNT 2
declare @i int
set @i = 100

WHILE @i > 0
BEGIN
UPDATE InABatch SET col1 = 1 WHERE Col1 = 0
SET @i = @@ROWCOUNT
END


(2 row(s) affected)


(2 row(s) affected)


(1 row(s) affected)


(0 row(s) affected)


set rowcount 0
select * from InABatch



--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
www.Konesans.com


"Karthik R" <dtsguy1 (AT) rediffmail (DOT) com> wrote

Quote:
Hi,
I want to do run a very large UPDATE Statement covering millions of
records. I saw some material in the web that shows how to acheive this
using SET ROWCOUNT, Infinite WHILE LOOP and @@ROWCOUNT.

I am not getting the approach. How can i write a update statement to
UPDATE records batch by batch to minimize transaction logging using
T-SQL?

Any pointers will be of great help.

Thanks in advance



Reply With Quote
  #3  
Old   
Karthik R
 
Posts: n/a

Default Re: T-SQL: Batch update - 10-08-2004 , 01:34 PM



"Allan Mitchell" <allan (AT) no-spam (DOT) sqldts.com> wrote

Quote:
The idea is that after each batch there are less items to update and
therefore when @@rowcount gets to 0 then you are finished

Imagine we have a table

CREATE TABLE InABatch(col1 int)
GO
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)
INSERT InABatch VALUES(0)

We have five rows but i want to only update 2 rows at a time so i will have
to issue an UPDATE statement *4


SET ROWCOUNT 2
declare @i int
set @i = 100

WHILE @i > 0
BEGIN
UPDATE InABatch SET col1 = 1 WHERE Col1 = 0
SET @i = @@ROWCOUNT
END


(2 row(s) affected)


(2 row(s) affected)


(1 row(s) affected)


(0 row(s) affected)


set rowcount 0
select * from InABatch



--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
www.Konesans.com


"Karthik R" <dtsguy1 (AT) rediffmail (DOT) com> wrote in message
news:d30961f9.0410041031.6208ffb8 (AT) posting (DOT) google.com...
Hi,
I want to do run a very large UPDATE Statement covering millions of
records. I saw some material in the web that shows how to acheive this
using SET ROWCOUNT, Infinite WHILE LOOP and @@ROWCOUNT.

I am not getting the approach. How can i write a update statement to
UPDATE records batch by batch to minimize transaction logging using
T-SQL?

Any pointers will be of great help.

Thanks in advance
Thanks Allan! Thats Great!!


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.