Попытка заархивировать некоторые данные из одной таблицы базы данных в другую по соединенному соединению с сервером.
Пока что мы довольно близко подошли, однако в настоящее время получаем удивительное исключение транзакции.
Вот T-SQL:
USE ArchiveDatabase
-- Declare date we are archiving to
DECLARE @ArchiveDate DATETIME
SET @ArchiveDate = DATEADD(MONTH, -2, GETDATE())
-- Create temp table
CREATE TABLE #DeleteIDs (ID int)
-- Continue looping while rows exist
WHILE EXISTS (SELECT TOP 1 * FROM [LINKEDSERVER].MasterDatabase.dbo.Logging WITH(NOLOCK) WHERE [Date] < @ArchiveDate)
BEGIN
BEGIN TRANSACTION
-- Get batch of IDs to archive
INSERT INTO #DeleteIDs ([ID])
(
SELECT TOP 1000 [ID]
FROM [LINKEDSERVER].MasterDatabase.dbo.Logging WITH(NOLOCK)
WHERE [Date] < @ArchiveDate
)
-- Archive where ID is within our current batch
INSERT INTO ArchiveDatabase.dbo.DataConnect_Logging
([Date],[Description],[AccountName],[ConnectionIPAddress],[HttpStatusCode],[HttpMethod],[Url])
(
SELECT [Date],[Description],[AccountName],[ConnectionIPAddress],[HttpStatusCode],[HttpMethod],[Url]
FROM [LINKEDSERVER].MasterDatabase.dbo.Logging WITH(NOLOCK)
WHERE ID IN
(
SELECT ID FROM #DeleteIDs
)
)
-- Remove where ID is within our current batch
DELETE FROM [LINKEDSERVER].MasterDatabase.dbo.Logging
WHERE ID IN (SELECT ID FROM #DeleteIDs)
-- Empty IDs
DELETE FROM #DeleteIDs
WAITFOR DELAY '00:00:02'
-- Any errors, roll back, otherwise commit
IF @@Error <> 0
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
END
DROP TABLE #DeleteIDs
И вот исключение:
(1000 row(s) affected)
(1000 row(s) affected)
OLE DB provider "SQLNCLI" for linked server "LINKEDSERVER" returned message "Cannot start more transactions on this session.".
Msg 7395, Level 16, State 2, Line 38
Unable to start a nested transaction for OLE DB provider "SQLNCLI" for linked server "LINKEDSERVER". A nested transaction was required because the XACT_ABORT option was set to OFF.