Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4098498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:18:35+00:00 2026-05-20T20:18:35+00:00

Trying to archive some data from one database table to another over linked server

  • 0

Trying to archive some data from one database table to another over linked server connection.

So far have gotten fairly close however getting a puzzling transaction exception currently.

Here’s the 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

And here’s the exception:

(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. 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:18:36+00:00Added an answer on May 20, 2026 at 8:18 pm

    Have you tried setting XACT_ABORT to on at the start of your script?

    SET XACT_ABORT ON
    

    Unrelated, but couldn’t the loop be written as:

    -- Continue looping while rows exist
    WHILE EXISTS (SELECT 1 FROM [LINKEDSERVER].MasterDatabase.dbo.Logging WITH(NOLOCK) 
                  WHERE [Date] < @ArchiveDate)
    

    Or even better, rewrite so that you don’t hit the linked table twice per loop iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like this trying to get some data from a Documents
I'm trying to import some data from excel into a database . i got
I am trying to ultimately extract some files from a tar archive within PHP,
I'm trying to use SharpZipLib to pull specified files from a zip archive. All
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I have a CSV export script (enrdata_arch.php) which calls information from an existing database
I'm trying to store some custom class/data to a file in my iPhone/iPad app.
I am trying to implement a rails Recipe application from the site http://oreilly.com/ruby/archive/rails-revisited.html .
I have been investigating for some time now a way to prevent my user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.