Home » Blog » SQL Server » How to Copy SQL Database From One Server to Another?

How to Copy SQL Database From One Server to Another?

Neeraj Kumar
Published By Neeraj Kumar
Debasish Pramanik
Approved By Debasish Pramanik
Published On
Last Updated

You can copy a SQL database from one server to another with five methods. Copy only backup, detach and attach, Copy Database Wizard and BACPAC files come with SQL Server. EmailDoctor migration software copies a database from an MDF file to a new server. Copy only backups protect the backup chain of the source. Verify the new database state once the copy ends.

Your tester needs last night's production data and the QA server sits in another building. Normal backups reset the differential base that the nightly backups depend on.

This guide explains five ways to copy a SQL database from one server to another, with scripts that you can try and the limits of every method.

Microsoft warns in its database detach and attach article that you need to recreate logins and scheduled tasks on the other server instance.

What Does a Database Copy Include?

Database copies include the data file and the log with every object stored inside them. Tables, views, stored procedures, users and permissions travel with the files while server logins and Agent tasks remain outside.

According to Microsoft, a BACPAC file stores SQL Server Authentication logins without a password. Imports create every login as a disabled login with a generated password.

Which Method can Copy a SQL Database From One Server to Another?

Select the method that matches your situation:

SituationMethod
Source must remain online with its backup chain protectedCopy only backup
Source can go offline and you want the fastest copyDetach and attach
You have sysadmin rights on both servers and want loginsCopy Database Wizard
You need one portable fileBACPAC
You only have an MDF fileMigration software

Four checks come before any copy:

  1. Match the destination release to the source or choose a newer one.
  2. Confirm free space on the destination for the data file and the log.
  3. Note the collation and the owner of the database.
  4. List the logins that the application uses.

5 Ways to Copy SQL Database to Another Server

Four methods rely on Microsoft tools and the fifth adds software for MDF files.

MethodSource databaseMovesMain limit
Copy Only BackupOnlineFull databaseLogins move separately
Detach and AttachOffline during the copyData and log filesDatabase unavailable to users
Copy Database WizardOnline with SMO and offline with detachDatabase plus chosen server objectsNot available in Express edition
BACPACOnlineSchema and data in one fileUnsupported objects stop the export
Migration SoftwareUntouched when you open a duplicateSchema and data from an MDF fileTrial record limit

Method #1: Copy Only Backup and Restore (Free, Protects the Backup Chain)

Copy only backups copy the database without touching the regular backup sequence. Microsoft states in its copy only backups article that a copy only full backup does not affect the differential base. Six steps copy SalesDB:

  1. Open the Tasks menu of SalesDB in Object Explorer and select Back Up.
  2. Tick Copy-only backup in the Source section of the General page.
  3. Add a file path under Destination and click OK.
  4. Copy the backup file to a folder that the destination server can open.
  5. Right click Databases at the destination and select Restore Database.
  6. Select Device, add the backup file and click OK.

T-SQL needs one extra option for the same backup:

BACKUP DATABASE SalesDB
TO DISK = N'E:\BAK\SalesDB_Copy.bak'
WITH COPY_ONLY, CHECKSUM;

Microsoft states that copy only backups never truncate the transaction log. Schedule a regular log backup when the source uses the full recovery model.

Method #2: Detach and Attach (Free, Source Goes Offline)

Detach takes the database out of the source instance and its data and log files remain in their folder. Copy those files and attach them on the destination server. Six conditions stop a detach according to Microsoft:

  • Replicated and published databases.
  • Databases with snapshots.
  • Members of an Always On availability group.
  • Databases in a mirroring session.
  • Suspect databases.
  • System databases.

Six steps move SalesDB:

  1. Open the Tasks menu of SalesDB and select Detach.
  2. Tick Drop Connections when users are connected and click OK.
  3. Copy SalesDB.mdf and SalesDB_log.ldf to the destination server.
  4. Right click Databases on the new instance and select Attach.
  5. Click Add and select the MDF file.
  6. Confirm the file paths and click OK.

T-SQL needs two statements for the same move:

EXEC sp_detach_db @dbname = N'SalesDB';

CREATE DATABASE SalesDB
ON (FILENAME = N'E:\Data\SalesDB.mdf'),
   (FILENAME = N'F:\Logs\SalesDB_log.ldf')
FOR ATTACH;

Note: Microsoft states that all data files must be available during the attach and that a database from a newer release cannot attach to an earlier one. Attach the files on the source again after the copy because detach removes the database from that instance.

Method #3: Copy Database Wizard (Free, Needs Sysadmin Rights)

Copy Database Wizard copies a database and optionally its logins and Agent tasks in one dialog. Microsoft lists three requirements on its Copy Database Wizard page:

  • Your destination server has the Agent service started.
  • Your login has sysadmin rights on both servers.
  • Your edition is above Express.

Five steps start a copy:

  1. Select SalesDB and choose Copy Database from Tasks.
  2. Enter the destination server and authentication and click Next.
  3. Select the transfer method and click Next.
  4. Select the database and mark Copy to protect the source.
  5. Add the logins you need under server objects and click Finish.

Note: The detach and attach method needs an SSIS proxy on the destination server. Microsoft states that without the proxy the process can fail and strip NTFS permissions from the source files. Details of both transfer methods sit in Transfer Data Between Two SQL Servers.

Method #4: Export and Import a BACPAC File (Free, One Portable File)

BACPAC files store the schema and the data of a database. Microsoft states in its import a BACPAC file article that the import creates a new database from the stored model and then bulk copies the data. Eight steps copy SalesDB:

  1. Right click SalesDB and select Tasks and then Export Data-tier Application.
  2. Select Save to local disk on Export Settings and type a path that ends in .bacpac.
  3. Click Next and then Finish.
  4. Copy the BACPAC file to the destination server.
  5. Right click Databases and select Import Data-tier Application.
  6. Select Import from local disk on Import Settings and browse to the file.
  7. Type the New database name and the data and log file paths on Database Settings.
  8. Click Next and then Finish.

SQL Server Authentication logins arrive disabled. Enable every login and set a new password:

ALTER LOGIN AppLogin ENABLE;
ALTER LOGIN AppLogin WITH PASSWORD = N'<new password>';

Note: According to Microsoft, the source server needs SQL Server 2005 SP4 or later. Databases with objects that a data tier application does not support cannot export.

Method #5: Copy a Database From an MDF File With EmailDoctor (Preview Free, Export Needs a License)

Some copies start with a loose MDF file and no working source server. EmailDoctor SQL Server Data Migration Tool opens the file and exports its objects as a database on a different server.

Work on a duplicate so the original MDF file remains untouched and copy it to a separate folder first. EmailDoctor software sets two trial limits at 25 records per table and five other categories. Licensed versions export without that cap.

Open the MDF File and Preview Objects

Nine steps open the file and show its objects:

  1. Launch the software on your Windows PC and choose Offline mode. Offline mode window
  2. Click Browse and add the file. Browse button for the MDF file
  3. Choose Quick Scan when the file is healthy and Advance Scan when it is damaged. Scan mode options
  4. Accept the detected SQL Server version or select one yourself. SQL Server version list
  5. Click OK to open the file. OK button after the scan settings
  6. Click Close after the scan finishes. Scan completed message
  7. Click Yes at the save prompt. Save scanned file prompt
  8. Save the scanned file in .str format and click OK. Save window for the STR file
  9. Select an object in the tree to see its rows. Preview of the MDF file objects

Export the Objects to the Destination

Eleven steps export the objects to the destination:

  1. Click Export at the top. Export button
  2. Select the migration type SQL Server Database. SQL Server Database export type
  3. Type the Server Name with Authentication values for the destination. Destination server login window
  4. Select the destination database. Destination database options
  5. Adjust Collation Settings if the application depends on them.
  6. Tick the objects that you want in the new database. Database object selection
  7. Choose With only Schema for structure alone or With Schema & Data for rows too. Schema and data options
  8. Click Save. Save button for the export
  9. Close the Export Completed message by pressing OK. Export Completed message
  10. Click Close and then Yes to save the report. Save report prompt
  11. Select a Location and click OK.

Note: Online mode copies a live database without an MDF file. Steps for it sit in Transfer Data Between Two SQL Servers.

Which Checks Follow the Copy?

Check the state, the owner and the statistics of the new database. Three checks finish the copy:

  1. Confirm that the database is online:
SELECT name, state_desc, compatibility_level, collation_name
FROM sys.databases
WHERE name = N'SalesDB';
  1. Set the owner. Microsoft states that detach and attach sets the owner to the login that ran the wizard:
ALTER AUTHORIZATION ON DATABASE::SalesDB TO [CONTOSO\DbOwner];
  1. Update the statistics as Microsoft recommends after a move:
USE SalesDB;
EXEC sp_updatestats;

Which Problems Stop the Copy of a SQL Database?

Five problems stop a copy:

ProblemReasonFix
Detach failsUsers have open sessionsSelect Drop Connections for every open session in the detach dialog
Attach reports a version errorThe source release is newer than the destinationUse a destination with an equal or newer release
Attach reports access deniedThe SQL Server service account cannot open the folderGrant the account access to the folder
BACPAC export failsThe database has objects that a data tier application does not supportRemove those objects or use a backup
Source files lose permissionsThe SSIS proxy is missing for detach and attachReapply the permissions and attach the source again

Frequently Asked Questions

Does a Copy-Only Backup Break the Differential Chain?

No. Microsoft states that a copy only full backup cannot serve as a differential base and does not affect the base of regular backups.

Can I Copy a Database While Users Work?

Yes. Copy only backups and BACPAC exports work while the database is online. Detach and attach needs the database offline.

Can I Attach a Newer Database to an Older Instance?

No. As per Microsoft, a database from a more recent release cannot attach to an earlier version.

Does a BACPAC File Include Logins?

Partly. SQL Server Authentication logins come with no password and arrive disabled. Windows logins need no extra step.

Does Detach Remove the Files From Disk?

No. Detach takes the database out of the instance and the files remain in their folder. Attach them again on the same instance or on a new one.

Can I Copy a System Database?

No. According to Microsoft, the wizard does not accept master, model, msdb or tempdb.

Can Migration Software Copy a Database When the Source Server is Down?

Yes. Select Offline mode and add a duplicate of the file. Then export the objects to the destination server.

Does the Trial Version of EmailDoctor Software Export Every Record?

No. The trial exports 25 records per table and five other categories. Licensed versions export without that cap.

Author’s Verdict

Copy only backups protect the backup chain of the source and suit databases that remain online. Use them to copy a SQL database from one server to another without a break in the nightly backups. Detach and attach fits a quiet window when speed matters.

Copy Database Wizard adds logins but needs sysadmin rights on both servers. BACPAC files suit one portable package.

Check the owner and the statistics before users connect to the copy. Migration software handles MDF files when no source server exists.

  • Five methods copy SQL databases between servers.
  • Copy only backups protect the differential base of the source.
  • Detach and attach needs an offline source.
  • Copy Database Wizard adds logins and Agent tasks.
  • BACPAC files store schema and data with disabled SQL logins.
  • Migration software opens MDF files.
  • Check state, owner and statistics on the new database.
  Neeraj Kumar

By Neeraj Kumar

Neeraj Kumar is a Principal Technical Architect at EmailDoctor with 19+ years of experience in email migration, cloud migration, data recovery and digital forensics. He guides enterprise teams through Exchange, Office 365 and Google Workspace projects and shares what works as an EmailDoctor writer. Every method in his posts is tested on Windows and Mac before it goes live.