Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Copy A SQL Server Database With Just The Objects and No Data

Uploaded by

osama17sadeq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Copy A SQL Server Database With Just The Objects and No Data

Uploaded by

osama17sadeq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

(https://www.mssqltips.

com/) MENU

Copy a SQL Server database with just the objects


and no data

By: Jeffrey Yao (/sqlserverauthor/121/jeffrey-yao/) | Updated: 2017-02-01 | Comments (9) | Related:


More (/sql-server-dba-resources/) > Database Administration (/sql-server-tip-category/60/database-
administration/)

(/sql-server-webcast-signup/?id=897&src=tip)

Free MSSQLTips Webinar: How to Avoid Worst Practices as a SQL Server DBA
(/sql-server-webcast-signup/?id=897&src=tip)
Learn from 20 years of experience about how to turn SQL Server Worst Practices such as database
corruption, security, scaling, monitoring, cloud cost management, and SQL Agent monitoring into Best
Practices to properly manage your database environment.

Problem
I 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?

Solution
There 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 (https://technet.microsoft.com/en-us/library/ee210546.aspx) 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 (https://msdn.microsoft.com/en-us/library/mt204009.aspx).

Backup a SQL Server Database without Data


Here are the detailed steps to do this using SQL Server Management Studio (/sql-server-tip-category/52/sql-
server-management-studio/) (SSMS).

1. In the SSMS Object Explorer Window, right click on the "AdventureWorks2012" database and choose
"Tasks" > "Extract Data-tier Application...", as shown below
.

2. 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:
3. Continue to click "Next" until you see the dacpac file generated successfully as shown below
.
4. Click the "Next" button to close the window.

Now I have an AdventureWorks2012.dacpac file generated.

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 package


The 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:

1. 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
.

2. 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
.

3. Click Next, and in the [Update Configuration] screen, input the required destination database name or
leave it as (defaults to the source database name)
.
4. Click Next until the Wizard starts to deploy to the destination database as shown below
.

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
(http://www.networkworld.com/article/3143196/windows/microsoft-is-replacing-the-cmd-prompt-with-
powershell.html).

The key here is to use SQLPackage.exe (https://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx),


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.e

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 l

sqlpackage /a:extract /of:true /scs:"server=localhost;database=AdventureWorks2012;trusted_connecti

<#
# if you have multiple databases to backup, you can generate their dacpac files easily using Power
# you may need to change server=localhost to your desired sql instance name (or put this instance

[string[]]$dbs = "db1", "db2";


$dbs | % {sqlpackage /a:extract /of:true /scs:"server=localhost;database=$_;trusted_connection=tru

#>

#do a restore to my local named instance


sqlpackage /a:publish /sf:"C:\SQL Server Management Studio\DAC Packages\AdventureWorks2012.dacpac"

<#
# if you have multiple databases to restore, you can publish these dacpac files easily using Power
# you may need to change server=localhost to your desired sql instance name (or put this instance

[string[]]$dbs = "db1", "db2";


$dbs | % {sqlpackage /a:publish /sf:"C:\SQL Server Management Studio\DAC Packages\$_.dacpac" /tcs:

#>
 

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.

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.
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 see
that the child table is created first before the parent table, and thus the model.sql execution fails.

Summary
Backing up a database without data is not difficult and can be implemented in multiple ways, however some
methods may be easier or more efficient than others.

To me, the DAC package file is probably the best way. I like its brevity and quickness, especially from an
automation perspective (for multiple databases). If you are a developer, there is an additional benefit that this
DAC package file can be immediately consumed by a database project in Visual Studio 2012+.

According to MSDN, DAC supports many database objects as listed here (https://msdn.microsoft.com/en-
us/library/ee210549.aspx), this implies some objects are not supported, but I find the list is not true. For
example, the SEQUENCE object is not listed here as supported, but I can find it created in the model.sql after
I unpack the dacpac file and I also see it after I restore the dacpac file to a new database. Same for some
other objects like CERTIFICATE.

Since Microsoft does not say DAC will support all database objects, you need to be cautious and test your
solution to see whether there are any objects that you need that are not included in the DAC package file.

Next Steps
SQLPackage.Exe can do some other interesting work, such as backing up a database with data, creating an
incremental update script that updates the schema of a target to match the schema of a source.

You may refer the following articles to better understand the DAC framework:

• Data-tier Applications (https://msdn.microsoft.com/en-ca/library/ee210546.aspx)


• Introduction to Data Tier Applications in SQL Server 2008 R2 (/sqlservertip/2100/introduction-to-data-tier-
applications-in-sql-server-2008-r2/)
• SqlPackage.Exe details (https://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx)

Related Articles
How to rename a SQL Server database (/sqlservertip/1122/how-to-rename-a-sql-server-database/)

How to determine SQL Server database transaction log usage (/sqlservertip/1225/how-to-determine-sql-server-database-transaction-log-usage/)

Determine Free Space, Consumed Space and Total Space Allocated for SQL Server databases (/sqlservertip/1629/determine-free-space-consumed-space-and-
total-space-allocated-for-sql-server-databases/)
Move SQL Server transaction log files to a different location via TSQL and SSMS (/sqlservertip/1774/move-sql-server-transaction-log-files-to-a-different-location-
via-tsql-and-ssms/)

Out of space on the C drive of your SQL Server and ways to reclaim disk space (/sqlservertip/1810/out-of-space-on-the-c-drive-of-your-sql-server-and-ways-to-
reclaim-disk-space/)

Attach SQL Server Database Without Log File (/sqlservertip/1894/attach-sql-server-database-without-log-file/)

Script to Get Available and Free Disk Space for SQL Server (/sqlservertip/2444/script-to-get-available-and-free-disk-space-for-sql-server/)

How to read the SQL Server Database Transaction Log (/sqlservertip/3076/how-to-read-the-sql-server-database-transaction-log/)

How to find user who ran DROP or DELETE statements on your SQL Server Objects (/sqlservertip/3090/how-to-find-user-who-ran-drop-or-delete-statements-
on-your-sql-server-objects/)

Understanding how SQL Server stores data in data files (/sqlservertip/4345/understanding-how-sql-server-stores-data-in-data-files/)

Renaming Physical Database File Names for a SQL Server Database (/sqlservertip/4419/renaming-physical-database-file-names-for-a-sql-server-database/)

Execute SQL Server Script Files with the sqlcmd Utility (/sqlservertip/4924/execute-sql-server-script-files-with-the-sqlcmd-utility/)

How to Check Monthly Growth of Database in SQL Server (/sqlservertip/6158/how-to-check-monthly-growth-of-database-in-sql-server/)

SQL Server Attach and Detach Database Examples (/sqlservertip/6222/sql-server-attach-and-detach-database-examples/)

How to stop and start SQL Server services (/sqlservertip/6307/how-to-stop-and-start-sql-server-services/)

How to Take SQL Server Database Offline (/sqlservertip/6418/how-to-take-sql-server-database-offline/)

How to Bring SQL Server Database Online (/sqlservertip/6419/how-to-bring-sql-server-database-online/)

SQL Server Download Quick Links (/sqlservertip/6429/sql-server-download-quick-links/)

SQL Server Move Database Files Step By Step (/sqlservertip/6689/sql-server-move-database-files/)

How to Shrink SQL Server Database Files (/sqlservertip/6888/shrink-database-sql-server/)

Popular Articles
Date and Time Conversions Using SQL Server (/sqlservertip/1145/date-and-time-conversions-using-sql-server/)

Format SQL Server Dates with FORMAT Function (/sqlservertip/2655/format-sql-server-dates-with-format-function/)

SQL Server Cursor Example (/sqlservertip/1599/cursor-in-sql-server/)

SQL Server CROSS APPLY and OUTER APPLY (/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/)

SQL Server DROP TABLE IF EXISTS Examples (/sqlservertip/6769/sql-server-drop-table-if-exists/)

Rolling up multiple rows into a single row and column for SQL Server data (/sqlservertip/2914/rolling-up-multiple-rows-into-a-single-row-and-column-for-sql-
server-data/)

How to tell what SQL Server versions you are running (/sqlservertip/1140/how-to-tell-what-sql-server-version-you-are-running/)

SQL NOT IN Operator (/sqlservertip/6904/sql-not-in-operator/)

Add and Subtract Dates using DATEADD in SQL Server (/sqlservertip/2509/add-and-subtract-dates-using-dateadd-in-sql-server/)

SQL Server Loop through Table Rows without Cursor (/sqlservertip/6148/sql-server-loop-through-table-rows-without-cursor/)

SQL Convert Date to YYYYMMDD (/sqlservertip/6452/sql-convert-date-to-yyyymmdd/)

Resolving could not open a connection to SQL Server errors (/sqlservertip/2340/resolving-could-not-open-a-connection-to-sql-server-errors/)

Concatenate SQL Server Columns into a String with CONCAT() (/sqlservertip/2985/concatenate-sql-server-columns-into-a-string-with-concat/)

Using MERGE in SQL Server to insert, update and delete at the same time (/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-
same-time/)

How to Get Current Date in SQL Server (/sqlservertip/6817/sql-current-date/)

SQL Server Row Count for all Tables in a Database (/sqlservertip/2537/sql-server-row-count-for-all-tables-in-a-database/)

Ways to compare and find differences for SQL Server tables and data (/sqlservertip/2779/ways-to-compare-and-find-differences-for-sql-server-tables-and-data/)

SQL Server Database Stuck in Restoring State (/sqlservertip/5460/sql-server-database-stuck-in-restoring-state/)

Searching and finding a string value in all columns in a SQL Server table (/sqlservertip/1522/searching-and-finding-a-string-value-in-all-columns-in-a-sql-server-
table/)

Execute Dynamic SQL commands in SQL Server (/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/)


About the author
(/sqlserverauthor/121/jeffrey-yao/) Jeffrey Yao is a senior SQL Server consultant, striving to automate DBA work as
much as possible to have more time for family, life and more automation.

View all my tips (/sqlserverauthor/121/jeffrey-yao/)

Article Last Updated: 2017-02-01

You might also like