Welcome to SqlAdvice Sign in | Join | Help

Archive and Compress Data from SQL Server

I recently blogged about deleting off data from a table by looping through one day at a time.  Strangely enough I had a similar need pop up a few days later although this time I wanted to keep the data around just in case I need it later.  I really didn't want to put the data into another table so I decided it might be nice to dump the data to a file and then zip it up into a zip file per day.  This way if I ever need the data again I can easily bcp the data back into the original table.  On my server I actually

This actually was pretty straightforward to do.  Basically we can just loop through the data by a given interval (in my case I chose a day), BCP the data to a file, zip the file, and finally delete the data.  Here is what my code looks like.  Once again I used a numbers table as in my previous post.  One item to note is that you will need some executable to do the zipping of the file.  In this case I just used zip.exe.  Also if you are on 2005 you will need to enable xp_cmdshell as well.

declare @startdate datetime declare @EndDate datetime declare @period datetime declare @QueueVendorOrderActivityID uniqueIdentifier declare @cmd varchar(1000) set @startdate = '9/01/2007' set @enddate = '9/15/2007' declare datecursor cursor forward_only read_only for Select dateadd(d,Number,@startdate) from Numbers where Number <= datediff(d, @startdate, @enddate) order by Number open datecursor while (1=1) begin fetch next from datecursor into @period if @@fetch_status <> 0 break; -- build cmd string to execute for bcping out the data set @cmd = 'bcp "select * from MyTable where createdate between ''' + convert(varchar(50), @period, 112) + ''' and dateadd(d, 1, ''' + convert(varchar(50), @period, 112) + ''')" queryout c:\MyTable' + convert(varchar(50), convert(varchar(50), @period, 112), 112) + '.dat -Umyuser -Pmypass -c' exec xp_cmdshell @cmd -- Now zip the file that we created. set @cmd = 'c:\zip c:\MyTable' + convert(varchar(50), @period, 112) + '.zip c:\MyTable' + convert(varchar(50), @period, 112) + '.dat' exec xp_cmdshell @cmd -- Delete the raw file set @cmd = 'del c:\MyTable' + convert(varchar(50), @period, 112) + '.dat' exec xp_cmdshell @cmd -- delete all the data that we just archived out from the table. delete from MyTable where createdate between @period and dateadd(d, 1, @period) end close datecursor deallocate datecursor

So far this seems to be working fairly well.  Let me know if you see any issues with this or have any other tips. 

Sponsor
Published Thursday, September 27, 2007 8:11 AM by gstark

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Saturday, February 21, 2009 10:06 PM by Ahsan Farooqi

# Abt SQLreporting services200

Hi.

I m using Sqlserver Reporting services 2000.

I have scheduled the report that will be return large amount data (approximate 5 MB).

that's why when i try to sent mail from Outlook its become Fail.

So tell me how to compress my data in zip form.

If you have any solution provide ASAP.

Regrads

Ahsan

Wednesday, March 18, 2009 12:28 PM by WWWW

# re: Archive and Compress Data from SQL Server

I have scheduled the report that will be return large amount data (approximate 5 MB).

http://www.becomon.com

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below