2015-09-02 93 views
0

我想刪除5條記錄後,在存儲過程中添加等待延遲。 @rowstoremove = 5。我想通過一項工作來執行這個程序,並希望確定在一個小時左右的時間內有多少記錄被刪除。等待存儲過程中的延遲

任何幫助將不勝感激。

CREATE Procedure [dbo].DeleteRecords 
    @rowstoremove int = 0, 
    @executeOnce char(1) = 'Y' 
As 
Begin 
    Declare @err int, 
      @rowCount int, 
      @rowsToDelete int 

    -- Load records of lowest id for a given duplicate set into a temp table 
    select 
     Min(iD) keptID, UserId, AccountId, AddressId 
    into  
     #tmpKept 
    from 
     adm.useraccess 
    group by 
     UserId, AccountId, AddressId 
    order by 1 

    select * 
    into #tmpRemoved 
    from adm.useraccess A 
    where not exists (select keptID 
         from #tmpKept T 
         where T.keptID = A.ID) 

    select @rowsToDelete = count(*) 
    from #tmpRemoved 

    if @rowstoremove = 0 
     select @rowstoremove = @rowsToDelete 

    if @executeOnce = 'Y' 
    begin 
     select 
      'Job Starting at ' + convert(varchar(255), getdate()) + '. Deleting a total of ' + convert(varchar(255), @rowstoremove) + ' rows' 

     delete top (@rowstoremove) adm.useraccess 
     from adm.useraccess A, #tmpRemoved R 
     where A.ID = R.ID 

     set @rowCount = @@ROWCOUNT 

     select 'Duplicate rows removed: ' + convert(varchar(255), @rowCount) +' on: (' + convert(varchar(255), getdate()) + ')' 

     return 
    end 

    if @executeOnce !='Y' 
    begin 
     select 'Job Starting at ' + convert(varchar(255), getdate()) + '. Deleting a total of ' + convert(varchar(255), @rowsToDelete) +' rows in groups of ' + convert(varchar(255), @rowstoremove) + ' rows at a time' 

     set @rowCount = 1 

     -- Remove duplicate records 
     while @rowCount > 0 
     begin 
      delete top (@rowstoremove) adm.useraccess 
      from adm.useraccess A, #tmpRemoved R 
      where A.ID = R.ID 

      set @rowCount = @@ROWCOUNT 

      select 'Duplicate rows removed: ' + convert(varchar(255), @rowCount) +' on: (' + convert(varchar(255), getdate()) + ')' 
     end 
    end 
end 
+0

對於'@ executeOne'使用位,它的工作原理是1 - 真,0 - 假 – lad2025

+0

我不太明白你..你想創建運行一個小時的存儲過程?請不要這樣做! – ericpap

+0

如果要一次刪除五行,然後休眠5秒,則需要刪除TOP 5,然後WAITFOR DELAY,然後循環,直到所有預期行都被刪除。爲什麼你想要做到這一點超出了我的想法,但這就是你將如何做到這一點。 – Greg

回答