How to copy data into a backup table

Before you update or delete data from a table, it is always a good idea to make a backup just in case.  Using the INTO clause in your sql statement, allows you to do this.

Below you see we have a table called traffic_sources.  We need to make a backup of it before we delete out some rows.

c1

 

By putting the INTO clause in our select statement, and specifying a new table name (traffic_sources_backup), running this statement copies all of the rows into the new table.  You can also use a where statement if you just want certain rows or you can select just the columns you want.

For more information about the INTO clause, here is the link to Books Online:

https://docs.microsoft.com/en-us/sql/t-sql/queries/select-into-clause-transact-sql

 

select * into traffic_sources_backup from traffic_sources

 

c2

 

After the statement completes, we select from our new table traffic_sources_backup and see all of the data is there.

 

c3

 

If possible, before you make changes to a table, it is always wise to create a backup in case you need to recover the data.  If you are just modifying one table, the INTO clause easily allows you to create a quick backup.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s