How To: Make A Copy Of A SQL Server Database Table

I’ve been working on a couple of projects recently where I’ve had to retain legacy databases and integrate them into new websites.

In order to do this without damaging the original tables, I find it useful to make a copy of the original database table and use that for the development work. Since I do all of my bespoke CMS development on hosted Microsoft SQL Server databases, I had to hunt down a quick method to copy an existing database table into a new one.

Let’s say we have an old table (oldnews) containing news items for a website. The structure is generally sound, but we don’t want to risk any damage to the original table. The following syntax copies the data from oldnews to the new table newnews:

INSERT INTO newnews SELECT * FROM oldnews

Apparently, this will also copy data into an existing table, so if newnews already exists the data will be placed there. If the table doesn’t already exist, it will be created. At least, that’s how it worked for me!

You can even copy table data from a different (local?) database by specifying the full database path:

INSERT INTO newnews SELECT * FROM olddb.dbo.oldnews

If you’ve got any more insight into table copying techniques, drop your wisdom in the comments!

Trackback URL for this post:

http://gerardmcgarry.com/trackback/110

Comments

This is exactly what I need

This is exactly what I need to do however the tables are on differnt databases and I am not sure the path to specify.

Source is an ODBC to SAGE 2008 STOCK table

Destination is SQL Table.

Thanks.

Just a slight twist on

Just a slight twist on copying a table from one DB to the other...

If you wish to copy the table records in at the same time you could use the following command.

Select * INTO newnews FROM olddb.dbo.oldnews


Hope this helps someone.

Wayne


Post new comment

The content of this field is kept private and will not be shown publicly.