尝试通过链接服务器连接将一些数据从一个数据库表存档到另一个数据库表。
到目前为止已经相当接近,但目前正在遇到令人费解的交易异常。
这是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.
答案 0 :(得分:2)
您是否尝试在脚本开头设置XACT_ABORT
?
SET XACT_ABORT ON
不相关,但无法将循环写为:
-- Continue looping while rows exist
WHILE EXISTS (SELECT 1 FROM [LINKEDSERVER].MasterDatabase.dbo.Logging WITH(NOLOCK)
WHERE [Date] < @ArchiveDate)
甚至更好,重写,这样你就不会在每次循环迭代时点击链接表两次。