Sql Server中Waitfor delay 語句的使用

來源:互聯網
上載者:User

/* <<@ ALTER  Cleanup Scripts>> */
CREATE PROCEDURE [dbo].PP_Cleanup
 @daystoleave INT,
 @chunksize INT,
 @reportsize INT,
 @proc VARCHAR(30)
/*  There are four input parameters:                                            */
/*    1) daystoleave is the number which determines the date prior to whom all  */
/*       the records will be deleted                                            */
/*    2) chunksize is the number of records to be deleted in one transaction    */
/*    3) reportsize multiplied with chunkzise is the number of records deleted  */
/*       after which the report is printed                                      */ 
/*    4) proc is a stored procedure corresponding to a database Table           */
AS
BEGIN
DECLARE @i int
DECLARE @datetime datetime
DECLARE @rowzch varchar(80)
DECLARE @rowseff int
SELECT @i = 0
SELECT @datetime = dateadd(day, -@daystoleave, getdate())
SELECT @rowseff = @chunksize
/* Set system variable rowcount to @chunksize*/
SET rowcount @chunksize
WHILE @rowseff = @chunksize
BEGIN
  WAITFOR DELAY '000:00:01'
  BEGIN TRANSACTION
  EXEC @proc @datetime
  SELECT @rowseff = @@rowcount       /* @rowseff preserves @@rowcount */
  COMMIT TRANSACTION
  SELECT @i = @i + @rowseff
  IF (@i % @reportsize * @chunksize = 0)
    BEGIN   
      SELECT @rowzch = 'Deleted ' + CONVERT(VARCHAR(15), @i) + ' rows'
      PRINT @rowzch
    END
END
SELECT @rowzch = 'Memo: Total rows deleted = ' + CONVERT(CHAR(15), @i)
PRINT @rowzch
END

GO

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.