I'm looking for a solution how I can move rows from a large table in chunks of 1000 to different tables based on parts of a datetime value (as a monthly archive). I'm using MS SQL Server 2008.
Remus Rusanu provided the following solution here on stackoverflow Move SQL Server data in limited (1000 row) chunks for moving rows in chunks. Works like a charm :-)
WHILE 1=1
BEGIN
WITH messages AS (
SELECT TOP 1000 id, messageDatetime, message
FROM DemoData)
DELETE messages
OUTPUT DELETED.id, messageDatetime, message
INTO messageArchive;
IF (@@ROWCOUNT = 0)
BREAK;
END
What I need now ist the ability to move the rows to different tables depending on the month part of the messageDate.
- messages from september should be inserted into a table named messageArchive_09
- messages from october should be inserted into a table named messageArchive_10
- ...
Any Ideas?