By:Jeffrey Yao || Related Tips:More > Database Administration
ProblemI sometimes get requests to copy a database from one server to another without any data. I just need an empty database with just the schema information and no data, how can I do it?
SolutionThere are a few potential solutions to this problem, for example any of the following methods will work:
Script out the source database and then run the script against an empty target database to create all database objects that are in the source database Backup the source database and restore to the destination database and then delete all table data. Backup the source database, restore it to an intermediate database, truncate all tables in this intermediate database, and backup this intermediate database, then restore it to the final destination database.All these can work very well, but there is another simpler way that is not well-known to DBAs, that is to use a DAC package which is applicable to SQL Server 2008 R2 and later versions.
To use a DAC package, we first need to download and install the Data-tier Application Framework, aka DacFx, from here .
Backup a SQL Server Database without DataHere are the detailed steps to do this using SQL Server Management Studio (SSMS).
In the SSMS Object Explorer Window, right click on the "AdventureWorks2012" database and choose "Tasks" > "Extract Data-tier Application...", as shown below.
Image may be NSFW.
Clik here to view.

The [Extract Data-tier Application] wizard will start. Click next in the first [Introduction] screen, and in the next [Set Properties] screen, input the DAC package file location or leave it as it is, as shown below:
Image may be NSFW.
Clik here to view.

Continue to click "Next" until you see the dacpac file generated successfully as shown below
.
Image may be NSFW.
Clik here to view.

Click the "Next" button to close the window.
Now I have an AdventureWorks2012.dacpac file generated.
Image may be NSFW.
Clik here to view.

With the DAC package file ready, we can copy it to anywhere or put it in a shared folder and then we can restore it to a new destination database.
Restore a SQL Server Database from a DAC packageThe DAC package can be restored to a target SQL Server instance whose version is equal to or higher than that of the source SQL Server instance.
Note: I actually successfully restored a SQL Server 2014 DAC package (from AdventureWorks2014) to SQL Server 2012 instance without any errors.
Here are the restore steps:
In SSMS Object Explorer Window, connect to the target SQL Server instance, in the [Object Explorer] window, right click [Databases] , and choose "Deploy Data-tier Application...", as shown below.
Image may be NSFW.
Clik here to view.

The [Deploy Data-tier Application] wizard will start, Click next in the first [Introduction] screen, and in the [Select Package] screen, click the Browse button to find the DAC package file location
.
Image may be NSFW.
Clik here to view.

Click Next, and in the [Update Configuration] screen, input the required destination database name or leave it as (defaults to the source database name)
.
Image may be NSFW.
Clik here to view.

Click Next until the Wizard starts to deploy to the destination database as shown below
.
Image may be NSFW.
Clik here to view.

Backup and Restore Automation with PowerShell
Now we know how to do this backup and restore via SSMS, but if we want to do this for multiple databases on multiple SQL instances, we need to rely on an automated script. I will demonstrate this via PowerShell instead of CMD.exe, because Microsoft is replacing CMD.exe with PowerShell as mentioned here .
The key here is to use SQLPackage.exe , a command line utility included in DacFx.
Let's start a PowerShell 3.0+ ISE window and run the following script, you can select one line and press F8 to run the selected line.
#Add sqlpackage.exe location folder to environment, this is done once only[string]$path='C:\Program Files\Microsoft SQL Server\130\DAC\bin' ## the folder where sqlpackage.exe resides
if (-not ($env:path).Contains('C:\Program Files\Microsoft SQL Server\130\DAC\bin'))
{ $env:path = $env:Path + ';C:\Program Files\Microsoft SQL Server\130\DAC\bin'; }
# do a backup, i.e. generate the DAC package file, assuming the source sql server instance is my local server
sqlpackage /a:extract /of:true /scs:"server=localhost;database=AdventureWorks2012;trusted_connection=true" /tf:"C:\SQL Server Management Studio\DAC Packages\AdventureWorks2012.dacpac";
<#
# if you have multiple databases to backup, you can generate their dacpac files easily using PowerShell as follows:
# you may need to change server=localhost to your desired sql instance name (or put this instance name in a variable)
[string[]]$dbs = "db1", "db2";
$dbs | % {sqlpackage /a:extract /of:true /scs:"server=localhost;database=$_;trusted_connection=true" /tf:"C:\SQL Server Management Studio\DAC Packages\$_.dacpac";}
#>
#do a restore to my local named instance
sqlpackage /a:publish /sf:"C:\SQL Server Management Studio\DAC Packages\AdventureWorks2012.dacpac" /tcs:"server=localhost\sql2014;database=AdventureWorks2012;trusted_connection=true"
<#
# if you have multiple databases to restore, you can publish these dacpac files easily using PowerShell as follows:
# you may need to change server=localhost to your desired sql instance name (or put this instance name in a variable)
[string[]]$dbs = "db1", "db2";
$dbs | % {sqlpackage /a:publish /sf:"C:\SQL Server Management Studio\DAC Packages\$_.dacpac" /tcs:"server=localhost\sql2014;database=$_;trusted_connection=true";}
#>
There is another benefit of using a DAC package file, we can un-package it by right-clicking on it and choose Unpack as shown below.
Image may be NSFW.
Clik here to view.

The final result is that the DAC package is extracted to a folder, as shown below. The most interesting one is the model.sql , which contains all the database objects, such as tables, views, functions, stored procedures, etc.
Image may be NSFW.
Clik here to view.

You may want to run the model.sql directly to create all the target database objects, but this may fail because this model.sql file does not guarantee to generate the object creation script in the correct order. For example if you have two tables, one table (child table) refers to another table (parent table), in model.sql you may