138

I want to get a backup of a single table with its data from a database in SQL Server using a script.

How can I do that?

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
EBS
  • 1,663
  • 2
  • 16
  • 17

7 Answers7

205
SELECT * INTO mytable_backup FROM mytable

This makes a copy of table mytable, and every row in it, called mytable_backup.

(It will not copy any indices, constraints, etc. Just the structure and data).

(It will fail if you have an existing table named mytable_backup, so if you want to use this code regularly - for example, to backup daily or monthly - you'll need to run drop mytable_backup first. But consider avoiding this in those cases, and use SQL Server's proper official built-in backup tools instead. Those will provide better features. This is more for a temporary one-time backup before running a risky update script in test, etc).

MGOwen
  • 6,562
  • 13
  • 58
  • 67
73

You can use the "Generate script for database objects" feature on SSMS.

  1. Right click on the target database
  2. Select Tasks > Generate Scripts
  3. Choose desired table or specific object
  4. Hit the Advanced button
  5. Under General, choose value on the Types of data to script. You can select Data only, Schema only, and Schema and data. Schema and data includes both table creation and actual data on the generated script.
  6. Click Next until wizard is done
TylerH
  • 20,799
  • 66
  • 75
  • 101
Kent Aguilar
  • 5,048
  • 1
  • 33
  • 20
43

There are many ways you can take back of table.

  1. BCP (BULK COPY PROGRAM)
  2. Generate Table Script with data
  3. Make a copy of table using SELECT INTO, example here
  4. SAVE Table Data Directly in a Flat file
  5. Export Data using SSIS to any destination
Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
Vijay Hulmani
  • 969
  • 8
  • 17
  • 13
    While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – CodeMouse92 Apr 25 '16 at 23:46
  • If it's for daily backup, we can use replication to replicate the table you want to backup. Then, back up the subscriber database. – DBALUKE HUANG Mar 04 '20 at 20:49
  • @CodeMouse92 Agreed. I down voted because the answer does short and lacks explanation. – Kellen Stuart Jul 29 '20 at 15:59
  • This doesn't really answer the question of "how" to do it, just provides the different ways. – TylerH Oct 31 '22 at 13:54
27

You can create table script along with its data using following steps:

  1. Right click on the database.
  2. Select Tasks > Generate scripts ...
  3. Click next.
  4. Click next.
  5. In Table/View Options, set Script Data to True; then click next.
  6. Select the Tables checkbox and click next.
  7. Select your table name and click next.
  8. Click next until the wizard is done.

For more information, see Eric Johnson's blog.

Michael
  • 8,362
  • 6
  • 61
  • 88
Pankaj
  • 675
  • 4
  • 3
3

Put the table in its own filegroup. You can then use regular SQL Server built in backup to backup the filegroup in which in effect backs up the table.

To backup a filegroup see: https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-files-and-filegroups-sql-server

To create a table on a non-default filegroup (its easy) see: Create a table on a filegroup other than the default

benjamin moskovits
  • 5,261
  • 1
  • 12
  • 22
2

Another approach you can take if you need to back up a single table out of multiple tables in a database is:

  1. Generate script of specific table(s) from a database (Right-click database, click Task > Generate Scripts...

  2. Run the script in the query editor. You must change/add the first line (USE DatabaseName) in the script to a new database, to avoid getting the "Database already exists" error.

  3. Right-click on the newly created database, and click on Task > Back Up... The backup will contain the selected table(s) from the original database.

DavidG
  • 24,279
  • 14
  • 89
  • 82
Simpa
  • 151
  • 6
0

To get a copy in a file on the local file-system, this rickety utility from the Windows start button menu worked: "C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\DTSWizard.exe"

wwmbes
  • 301
  • 2
  • 4