How to move data one table to another table older than 30 days record? MS SQL SERVER 2016

Devendra Kumar Sahu 236 Reputation points
2022-02-09T09:45:40.19+00:00

I have two table OUTBOX and OUTBOX_1 both table column is same.

My table containing 14409004 record. I want to move record to OUTBOX_1 from OUTBOX those are Current date to older than 30 days.

Below the query through i fount out older data form 30 Days

SELECT * FROM OUTBOX WHERE DT_INSERT >= DATEADD(DAY, -30, GETDATE())

but i unable to move data

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,864 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,559 questions
{count} votes

Accepted answer
  1. Sreeju Nair 11,621 Reputation points
    2022-02-09T10:33:20.037+00:00

    Basically you need to do this ina transaction. Then use an insert into statement to insert data from table 1 to table 2, then delete the records from table 1, and then commit the transaction. An example

    Begin Transaction
    Insert into Outbox_1 (field1, field 2......) SELECT (field1, field2......) FROM OUTBOX WHERE DT_INSERT >= DATEADD(DAY, -30, GETDATE());
    Delete FROM OUTBOX WHERE DT_INSERT >= DATEADD(DAY, -30, GETDATE());
    
    Commit;
    

    Hope this helps

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful