How to Make a Copy of a Database on Windows Azure SQL Database

by Dec 11, 2011

Windows Azure SQL Database (previously SQL Azure) offers a unique way to make a database copy to another database using the CREATE DATABASE as COPY OF command. The copy operation is a mechanism that developers and administrators can use to clone a SQL Database instance on another SQL Database with transactional consistency.

Note, however, that this is not a backup mechanism since the cloned database cannot be restored. In addition, once the copy operation is completed you will incur charges on the newly created database. Nevertheless, it is the only mechanism available today that creates a copy of an existing database with transactional consistency.

The exact command to run depends on whether or not you are copying a database to the same SQL Database server or to a different one. To create a new database (new_db) as a copy of an existing database (current_db) on the same server, simply connect to the master database then run the following statement:

    CREATE DATABASE [new_db] AS COPY OF [current_db]

If current_db is to be created on another SQL Database server, connect to the destination server and run this command (where source_server is the SQL Database name where current_db is stored):

    CREATE DATABASE [new_db] AS COPY OF [source_server].[current_db]

Because this operation is asynchronous, the command returns immediately; however it could take a few minutes to complete, or even a few hours depending on the size of the database. To check the status of the copy operation, you can query the sys.dm_database_copies view from the master database as such:

    SELECT * FROM sys.dm_database_copies

This command will return a list of pending copy operations, including the destination database name, the completion percentage, and any errors that may have caused the copy operation to restart automatically. For details on this system view, check MSDN here:

There are some important considerations to keep in mind however:

  • The destination database cannot be accessed until the copy operation is completed
  • Only the server-level principal login or members of the dbmanager role can create databases
  • You can cancel the copy operation by dropping the destination database
  • The copy operation is not currently supported on SQL Database federation members
  • The maximum size of the destination database is the same as the original database
  • You can start multiple copy operations if needed

The copy operation is very useful to developers that need a copy of an existing database for testing or development purposes. It is also useful to safeguard your data, but should not be used as a backup solution. You can use the Azure SQL Database Backup tool to backup your SQL Database instances.