Explain Codes LogoExplain Codes Logo

Copy tables from one database to another in SQL Server

sql
data-transfer
sql-server
database-management
Anton ShumikhinbyAnton Shumikhin·Feb 3, 2025
TLDR

It's child's play to duplicate tables across SQL Server databases with the Generate Scripts tool in SSMS.

Short steps:

  1. Connect to SQL Server -> Database node -> Right-Click -> Tasks -> Generate Scripts.
  2. Select your tables to configure transfer -> Identify Types of data to script as both Schema and Data.
  3. Export the manifest -> Alter the USE directive to the destination database.
  4. Run that script on the destination to clone the tables.

Example:

USE YourDestinationDB; -- Change me to your target DB! -- Paste generated table schema and insert scripts here

This runs a table schema and data replication ensuring data reality stays fixed. Permissions and dependencies want checking before you commit to the move.

Go beyond: enhanced moves

Basic copying is like moving houses with a car; you're mostly able to carry everything, but the process is painful. Here are some moving vans:

Import and Export Wizard: your digital mover

SSMS's Import and Export Wizard is like hiring a digital moving company to do all the dirty work. It transfers data without breaking a sweat - or your tables.

  1. Open the wizard.
  2. Choose the database with your tables.
  3. Select a destination to accommodate the new data.
  4. Pick the tables and specify any transformations required, like flipping data upside down or giving it a new shape (just kidding!).

Moving big data? Use BCP or SSIS

We all have that one friend who owns way too much stuff. For them, aka large datasets, they'd need a Bulk Copy Program (BCP) or SQL Server Integration Services (SSIS).

Advanced options with scripting

Remember when we said the wizard is like a moving company? As in real life, **some items have to be handled more carefully, like indexes and constraints.

  • Script them separately using SSMS, or
  • Manually recreate them in the new house, I mean, database.

It keeps coming back! Use Linked Servers or replication

For persistent table cloning, consider:

  • Linked Servers for server to server data moves, or
  • Transactional replication or database snapshots for changes to be reflected duct-taped to the wall and screaming, "Mirror, mirror on the wall, who's the most up-to-date of them all?".

Keep it secret, keep it safe!

Permissions are shy creatures. They wouldn't like to move with the data:

  • Reestablish security settings in the new database.
  • Handle access controls post-transfer like you'd handle a white carpet in a kid's room - very carefully!

Hitchhiker's Guide to Data Transports

The SQL way or the highway

  • Favour SELECT INTO to create and copy data to new tables in the target database.
  • Gravitate towards INSERT INTO ... SELECT FROM statements if you're filling existing tables. Think of it as topping up your tea - it only works if your cup isn't already full.

Speed is everything

  • The Fast and Furious franchise wasn't about cars. It was about data transfers. Improve speed by using indexed views or partitioning.
  • Between the servers, it's not the space that matters, it’s the journey. Make it a smooth ride by optimizing for network performance.

Error handling

  • Always carry an umbrella! Preempt any performance showers with error handling procedures during transfers.
  • Test, test, test! Test it in the playground first – the non-production environment.