--Select Count(AsyncOperationId)from AsyncOperationBase WITH (NOLOCK)
--where OperationType in (1, 9, 12, 25, 27, 10)
--AND StateCode = 3 AND StatusCode IN (30,32)
declare
@starttime datetime=getDATE()
declare
@endtime datetime
declare
@iterationNumber int
declare
@iterationtime datetime
declare
@duration time
declare
@message nvarchar(max)
declare
@rowsPerIteration integer = 10000
declare
@rowsLeft int
declare
@totalrows int
declare
@totalAsyncLogsForDelete TABLE (
AsyncOperationId uniqueidentifier not null primary key
)
insert into
@totalAsyncLogsForDelete Select AsyncOperationId FROM ARCH_MSCRM.dbo.AsyncOperationBase (nolock)
where OperationType in (1, 9, 12, 25, 27, 10) and StateCode = 3 AND StatusCode IN (30,32)
set
@totalrows = (select count(*) from
@totalAsyncLogsForDelete)
set
@rowsLeft =
@totalrowswhile(
@rowsLeft > 0)
begin
begin try
begin tran ArchiveTran
set
@iterationNumber =
@iterationNumber + 1
set
@iterationtime = CURRENT_TIMESTAMP
-- Переменная для хранения сеансов, которые удаляются в рамках итерации
declare
@asyncLogsForDeletePerIteration TABLE (
AsyncOperationId uniqueidentifier not null primary key
)
-- Шаг 1. Сохраняем в переменную сеансы для удаления в текущей итерации
INSERT INTO
@asyncLogsForDeletePerIteration (AsyncOperationId)
Select top (
@rowsPerIteration) AsyncOperationId FROM
@totalAsyncLogsForDelete -- Шаг 2.1 Архивируем POA
select poa.* into Archive.dbo.PrincipalObjectAccess from ARCH_MSCRM.dbo.PrincipalObjectAccess poa
join ARCH_MSCRM.dbo.WorkflowLogBase wlb on
poa.ObjectId = wlb.WorkflowLogId
join
@asyncLogsForDeletePerIteration dart on
wlb.AsyncOperationId = dart.AsyncOperationId
-- Шаг 2.2 Удаляем POA
delete poa from ARCH_MSCRM.dbo.PrincipalObjectAccess poa
join ARCH_MSCRM.dbo.WorkflowLogBase (nolock) wlb on
poa.ObjectId = wlb.WorkflowLogId
join
@asyncLogsForDeletePerIteration dart on
wlb.AsyncOperationId = dart.AsyncOperationId
-- Шаг 3.1 Архивируем WorkflowLogBase
select w.* into Archive.dbo.WorkflowLogBase from WorkflowLogBase (nolock) W ,
@asyncLogsForDeletePerIteration d
where W.AsyncOperationId = d.AsyncOperationId
-- Шаг 3.2 Удаляем WorkflowLogBase
delete WorkflowLogBase from WorkflowLogBase W,
@asyncLogsForDeletePerIteration d
where W.AsyncOperationId = d.AsyncOperationId
-- Шаг 4.1 Архивируем BulkDeleteFailureBase
Select B.* into Archive.dbo.BulkDeleteFailureBase From BulkDeleteFailureBase (nolock) B,
@asyncLogsForDeletePerIteration d
where B.AsyncOperationId = d.AsyncOperationId
-- Шаг 4.2 Удаляем BulkDeleteFailureBase
delete BulkDeleteFailureBase From BulkDeleteFailureBase (nolock) B,
@asyncLogsForDeletePerIteration d
where B.AsyncOperationId = d.AsyncOperationId
-- Шаг 5.1 Архивируем BulkDeleteOperationBase
select O. * into Archive.dbo.BulkDeleteOperationBase From BulkDeleteOperationBase (nolock) O,
@asyncLogsForDeletePerIteration d
where O.AsyncOperationId = d.AsyncOperationId
-- Шаг 5.2 Удаляем BulkDeleteOperationBase
delete BulkDeleteOperationBase From BulkDeleteOperationBase (nolock) O,
@asyncLogsForDeletePerIteration d
where O.AsyncOperationId = d.AsyncOperationId
--Шаг 6.1 Архивируем WorkflowWaitSubscriptionBase
SELECT WS.* INTO Archive.dbo.WorkflowWaitSubscriptionBase from WorkflowWaitSubscriptionBase (nolock) WS,
@asyncLogsForDeletePerIteration d
where WS.AsyncOperationId = d.AsyncOperationID
--Шаг 6.2 Удаляем WorkflowWaitSubscriptionBase и архивируем
delete WorkflowWaitSubscriptionBase from WorkflowWaitSubscriptionBase WS,
@asyncLogsForDeletePerIteration d
where WS.AsyncOperationId = d.AsyncOperationID