2

I have an SQL table which is used for logging purpose(There are lakhs of records in the table). I need to purge the table (Take a back up of the data and need to clear the table data).

Is there a standard way of doing it where I can automate it.?

Chandra Eskay
  • 2,163
  • 9
  • 38
  • 58

2 Answers2

2

You can do this within SQL Server Management Studio, by:

right clicking Database > Tasks > Generate Script

You can then select the table you wish to script out and also choose to include any associated objects, such as constraints and indexes.

Attaching an image which will give you the step by step procedure,

image_bkp_procedure

PFB the stackoverflow link which will give you more insight on this,

Table-level backup

And your automation requirement,

You can download bcp utility which copies data between an instance of Microsoft SQL Server and a data file in a user-specified format. Sample syntax to export,

bcp "select * from [MyDatabase].dbo.Customer " queryout "Customer.bcp" -N -S localhost -T -E

You can automate this query by using any scheduling mechanism (UNIX etc)

Community
  • 1
  • 1
Mariappan Subramanian
  • 9,527
  • 8
  • 32
  • 33
2
Simply we can create a job that runs once in a month
--> That backups data in another table like archive table
--> Then deletes data in the main table
Its primitive partitioning I guess, this way it will be more flexible when you need to select data from the past deleted one i.e. now on archive table where you have backed up
Sandip Bantawa
  • 2,822
  • 4
  • 31
  • 47