Quantcast
Channel: SQL Server Training | Tutorials | How to's » Best Practices
Viewing all articles
Browse latest Browse all 10

How to backup SQL table ?

$
0
0

Backup SQL table, have you ever tried to backup a single SQL table inside a database? Let’s see How to backup SQL table | SQL Table Backup Restore

DOES SQL Server supports table level backups ?

Backup Types are dependent on SQL Server Recovery Model. Every recovery model lets you back up whole or partial SQL Server database or individual files or filegroups of the database. Table-level backup cannot be created, there is no such option. BUT there is a workaround  for this

Taking backup of SQL Server table possible in SQL Server. There are various alternative ways to backup a table in sql SQL Server

  1. BCP (BULK COPY PROGRAM)
  2. Generate Table Script with data
  3. Make a copy of table using SELECT INTO
  4. SAVE Table Data Directly in a Flat file
  5. Export Data using SSIS to any destination

Let’s see how we can use these methods to take table backup in sql server

To make it more clear, let’s take example, we want to backup SQL table named "Person.Contact", which resides in SQL Server AdventureWorks sample database, which has 19972 records and table size is 6888 KB

How to backup SQL table ?

Method 1 – Backup sql table using BCP (BULK COPY PROGRAM)

To backup a SQL table named "Person.Contact", which resides in SQL Server AdventureWorks, we need to execute following script, which

-- SQL Table Backup
-- Developed by DBATAG, www.DBATAG.com
DECLARE @table VARCHAR(128),
@file VARCHAR(255),
@cmd VARCHAR(512)
SET @table = 'AdventureWorks.Person.Contact' --  Table Name which you want to backup
SET @file = 'C:\MSSQL\Backup\' + @table + '_' + CONVERT(CHAR(8), GETDATE(), 112) --  Replace C:\MSSQL\Backup\ to destination dir where you want to place table data backup
+ '.dat'
SET @cmd = 'bcp ' + @table + ' out ' + @file + ' -n -T '
EXEC master..xp_cmdshell @cmd

OUTPUT

How to backup SQL table ?

Note -

  1. You must have bulk import / export privileges
  2. In above Script -n denotes native SQL data types, which is key during restore
  3. -T denotes that you are connecting to SQL Server using Windows Authentication, in case you want to connect using SQL Server Authentication use -U<username> -P<passord>
  4. This will also tell, you speed to data transfer, in my case this was 212468.08 rows per sec.
  5. Once this commands completes, this will create a file named "AdventureWorks.Person.Contact_20120222" is a specified destination folder

Alternatively, you can run the BCP via command prompt and type the following command in command prompt, both operation performs the same activity, but I like the above mentioned method as that’s save type in opening a command prompt and type.

bcp AdventureWorks.Person.Contact out C:\MSSQL\Backup\AdventureWorks.Person.Contact_20120222.dat -n -T 

How to backup SQL table ?

backup which can not restored is of no use, let’s perform a quick restore to verify that this table level backup do works….

Restore SQL table backup using BCP (BULK COPY PROGRAM)

The following script will help you to perform a table level restore, which we backed up in above steps

BULK INSERT AdventureWorks.Person.Contacts_Restore 
    FROM 'C:\MSSQL\Backup\Contact.Dat' 
    WITH (DATAFILETYPE='native'); 

Screen OUTPUT / Verification

How to backup SQL table ?

Method 2 – Backup sql table using Generate Table Script with data

To backup a SQL table named "Person.Contact", which resides in SQL Server AdventureWorks, can generate a Script for table schema as well as data.

Let’s quickly see, how we generate a Script for SQL table including SQL Data.

How to backup SQL table ?

How to backup SQL table ?

How to backup SQL table ?

Once you finish that you will Table Creation Script with Data insert statements, which is nothing just equal to dump statement in mysql databases.

In case of restore is required, just execute the Script which we generated.

Method 3 – Backup sql table using SELECT INTO

SELECT INTO statement selects data from one table and inserts selected data into a different table. This is nothing just like making a copy table. This will make a copy of a table inside a database only.

I do personally use this statement, prior to make changes to production database if table if of few MB’s. Don’t use this for large tables, this might fill up entire space of your database / drive.

The following Script will create a table name Contacts_Copy_20120221, and copy all data from table Contact to this newly created table.

select * into AdventureWorks.Person.Contacts_Copy_20120221 
    from AdventureWorks.Person.Contact

Method 4 – Backup sql table using SAVE Table Data Directly in a Flat file

When you execute any Select statement, SQL Server by default shows you result in result area, but we can change that option and set

when we execute a statement, sent the output to a flat file, instead of showing that on SSMS screen.

This is how we Backup sql table using SAVE Table Data Directly in a Flat file

How to backup SQL table ?

Method 5 - Backup sql table using SSIS package by exporting data to any Destination

I have already written a article on Transferring Data using Integration Services, please check this for more details on this.

The post How to backup SQL table ? appeared first on SQL Server Training | Tutorials | How to's.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images