Question : Set rowcount inside a Schaduled Job

I'm running a nightly delete job that runs for few minutes and produces Timeouts. Currently this is what I am doing in a SQL Server Job:

Delete FROM MyTable WHERE mydate < GetDate

I wanted to know if there are any issues to do the following in a schaduled job. I know that the following works in query analyzer. Basicly I want to limit the number of rows that are getting deleted by using Set rowcount.

Set rowcount 1000

Declare @Counter int
Set @Counter = 10

While @Counter > 0
Begin

      Delete FROM MyTable WHERE mydate < GetDate

Set @Counter = @Counter - 1
Print @Counter
End

      

Answer : Set rowcount inside a Schaduled Job

although, if you do this, it will delete all of the records 1000 at a time (even if there are more than 10000 records.

Set rowcount 1000

Declare @Counter int
SET @Counter = 1

While @Counter > 0
Begin

      Delete FROM MyTable WHERE mydate < GetDate

Set @Counter = @@ROWCOUNT
Print @Counter
End
Random Solutions  
 
programming4us programming4us