Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Get Table Data Output into a text file

$
0
0

Hi Experts,

I have below code. Which is taking data from 2 tables and archiving it to some archive database. After archived. Its deleting data from the production table for the specified data range... I want to take the count of archived data for both the tables into a text file e.g If table 1 has 100 rows and table 2 has 200 rows to archive it can put into text file that 

"Total 100 has been moved from T1"

"Total 200 has been moved from T2"

IF no rows satisfied the condition than "No data found fro transfer"

Any Help...


USE ADB
GO
SET NOCOUNT ON
DECLARE @ConditionDate DateTime;
SET @ConditionDate = DATEADD(mm,-12, CURRENT_TIMESTAMP)

BEGIN TRAN

-- Move Data To Archive Table

    INSERT INTO BDB.dbo.T1
        SELECT *
        FROM ADB.dbo.T1
    WHERE EDate < @ConditionDate
   
-- Delete Data From Original Table

    DELETE T1
    WHERE EDate < @ConditionDate

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN
        RAISERROR ('Error occured while moving data' , 16, 1)
     END

-- Move Data To Archive Table
    INSERT INTO BDB.dbo.T2
        SELECT *
        FROM ADB.dbo.T2
    WHERE EDate < @ConditionDate
   
-- Delete Data From Original Table

    DELETE T2
    WHERE EDate < @ConditionDate

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN
        RAISERROR ('Error occured while moving data' , 16, 1)
     END

  IF @@TRANCOUNT > 0
    BEGIN
        COMMIT TRAN 
    END


Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>