How to take Table backup in sql:2 Easy Methods

How to take Table backup in sql To take a backup of a table in SQL Server with the table’s schema (CREATE TABLE statement) and data (INSERT statements), In this article we will learn How to take Table backup in sql with 2 easy methods

How to take table backup in SQL | Generate Script in SQL Server (youtube.com)


Follow the below steps:
You can take backup of create statement and insert statements in two ways

  1. Table backup in sql Using Generate Script
  2. Table backup in sql Using a SQL query

Backup Table using Generate Script

There are two different options in Generate Script. You can generate a table backup for the create script (Schema) or the insert script (Data), or you can generate a create and insert script at the same time by using the schema and data options in Generate Script.
let us see one by one in details

Cloud Hosting Sql server

Generate the Create Table Script:

You can use SQL Server Management Studio (SSMS) or a SQL script to generate the CREATE TABLE statement for the table you want to back up. Here’s how you can do it in SSMS:

In SQL Server Management Studio (SSMS) you can generate Create Table script statement for which table you want to take a backup

How to take Table backup in sql

Steps to Create Table script backup

Step1. Open SQL server management Studio and login to your sql instance

Step2. In the Object Explorer, navigate to the databases and expand the database and choose the table which you want to take a backup.

Step3. Expand the “Tables” node to view the list of tables.

Step4. Right-click on the table you want to back up and choose “Script Table as” -> “CREATE To” -> “New Query Editor Window.”

Step5. This will generate a script that includes the CREATE TABLE statement. You can save this script for later use by default it will be saved in your documents with script.sql.

Generate the Create Table Script
generate create table script in ssms

Generate the INSERT Script:

To generate the INSERT script of the data in the table, you can use SQL Server’s built-in functionality or a custom sql script.
To generate insert script by using SSMS SQL Server Management Studio follow the below steps

Step1. Right-click on the database containing the table and select “Tasks” -> “Generate Scripts.”

generate script step 1

Step2. In the “Generate Scripts” wizard, select the table that you want to back up you can select multiple tables to generate script.

generate script  step 2 select table

Step3 .In the Choose object click on radio button “Select specific database objects” and then Expand the Tables and click on check box for table which you want to backup or script and click on Next.

Step4. In the “Set Scripting Options” step, click on Advanced option and choose Type of objects to script ‘Data only’.
here you will see the Three options.

1.Data only
In this ‘Data only’ option it will generate the insert scipt of that selected table
2.Schema and Data
In ‘Schema and Data’ option it will generate the insert scipt as well as create script of that selected table
3.Schema only
in Schema Only option it will generate only create statement script for selected table

step 4 generate script : select schema or data

Step5. Choose other options as needed and proceed to generate the script.

Step6. Save the script to a file, select path to save script file and click on Next button.

save script file

Step7. Click on Next button and then click on Finish, your file will be default saved in your servers document path or if you have given some different path then you will see the script file in your given path.

save script and finish

Combine the Scripts:

Open both scripts in a text editor or code editor, and combine them into a single script. Place the CREATE TABLE script at the beginning, with the INSERT statements.

If you want to take Table backup with create and insert script in single way then you can choose the “Schema and Data ” option while generating the script then you dont need to combine create and insert script it will automatically generate create statement at beginning, and insert statement after create statement.

Ensure that the CREATE TABLE script does not have any issues, such as foreign key constraints, that may affect the execution of INSERT statements.

Save the Combined Script:

Save the combined script as a .sql file. This script will create the table structure and insert the data when executed.

Execute the Backup Script:

To restore the table from the backup, you can execute the script in Sql server Management studio or using sql dbx tools which can run SQL scripts.

Click Here How to restore Database

Keep in mind that this approach may not be suitable for very large tables, as the script can become unwieldy or size of script will be larger it may hang your editor if large data file is backup up using generate script option.

for larger tables you can take a backup of your database.

Click Here How to take a database backup

How to take Table backup in sql

Backup Table by using Sql script

To take backup of table using sql query you can write Select * into new table from your old table which you want to take a backup this will create same copy of your table with schema and data (create and insert statement)

Syntax Insert into statement

Select * into [New Table1] from [Old table2];

Example Insert into statement

select * into Employee_Backup_table  from Employee

here we have taken backup of Employee table with new name Employee_Backup_table with create and insert script.

sql table backup uisng query

Leave a Comment