You can use the OUTPUT clause to delete and return the data to be moved in one go.
create table ProductionTable
(
ValueDate datetime not null
, Data varchar(max) not null
)
insert ProductionTable values ('20100101', '3 years ago')
insert ProductionTable values ('20130425', 'this year')
insert ProductionTable values ('20130426', 'this year')
delete ProductionTable
output deleted.ValueDate, deleted.Data
where ValueDate <= dateadd(year, -3, getdate())
The code can also be accessed on SQLFiddle
Now I will show you the exact steps you need to follow in SSIS to reproduce the example:
- Create a new project and define your data sources for ProductionDB and ArchiveDB.
- In "Control Flow" tab, create a "Data Flow Task".
- In "Data Flow" tab, create a "OLE DB Source" and a "OLE DB Destination".
- In "OLE DB Source", select ProductionDB and choose "SQL command" as the data access mode. Paste in the delete statement with the output clause.
- Click on "Columns" and then OK.
- In "OLE DB Destination", select ArchiveDB and choose "Table or view - fast load" as the data access mode and then choose your ArchiveTable.
- Click on "Mappings" and then Ok.
- Run the package and you should be able to verify that one row is deleted from ProductionTable and moved to ArchiveTable.
Hope it helps.
Other things to keep in mind, because you are deleting and moving data around, transactional consistency is very important. Imagine half way through your delete/move, the server went down, you then end up with data being deleted but not made it to the archive.
If you are unsure about how to protect your data by enforcing transactional consistency, please seek help from other SQL/SSIS experts on how to use transactions in SSIS.