I’ve been writing a bunch about Azure Data Studio. I’ve also been recording videos on the topic. A comment I received recently asked how to export a database from Azure Data Studio. It made me want to explore the topic of exporting a database as it relates to Azure Data Studio.
When we say export, what exactly do we mean. It could be as simple as exporting data to a flat file for consumption in Excel or something. It could be creating a backup. Maybe we mean creating a bacpac file. We could also be looking at creating individual scripts for objects within the database. Finally, what about a full export of the database object definitions? Any or all of these could be what the question was about. So, let’s quickly address them each.
Each of these could be a manual process or an automated process. Instead of trying to address all of these at the same time, I’m going to break them down to individual steps, unique to Azure Data Studio, and then summarize with a section on automation at the end.
Flat File Export
While there is an Extension that lets you import flat files, there is not yet a tool for reversing that process within Azure Data Studio. However, that doesn’t mean that there isn’t a way to get this done. Let’s say we want to get some data out to Excel and we can define a query (which intentionally has a * because we want to export all the columns):
SELECT * FROM Sales.SalesOrderHeader AS soh JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID WHERE soh.OrderDate > '2014-06-26';
If we execute the query within Azure Data Studio the results will look something like this (click to make bigger):
If you look all the way over to the right side of the screen, you’ll see this:
These are the export icons. In order they are;
- Save as CSV
- Save as Excel
- Save as JSON
- Save as XML
In short, there’s a pretty easy way to get a result set out to a flat file in a variety of formats.
This topic is pretty short. It’s SQL Server. Even though I’m running these examples against a Linux container in Docker, a backup is a backup is a backup. Scripting a backup in SQL Server doesn’t change a lick. There is a built-in backup utility in Azure Data Studio. Right click on a database and select Backup from the context menu (not at all dissimilar to working in SQL Server Management Studio). You’ll get a new window that looks like this (again, click to make bigger):
I’m not going to run through all of that. It’s a backup. You can see that all the standard settings for a backup are available. Finally, you can choose to either run the backup defined within the window or you can script it out.
I’m not a fan. However, the bacpac is one method of extracting a database definition or exporting an entire database, so it’s worth discussing. To get this functionality, you will need to install the SQL Server Import extension.
With that installed, you will see a couple of new context menus. The one we’re interested in is the “Data tier Application Wizard”. That will open the following:
There are two choices that interest us. First is the “Extract a data-tier application…”. This wizard will extract a dacpac that will contain all the structures of your database, but not the data. The second is the “Export the schema and data from a database…” wizard. This will create a bacpac, which is a dacpac, plus data. Extracting everything we need.
You also have the ability to create scripts. Within Azure Data Studio, right click on any object and the context menu will include “Script as Create.” This will generate your standard T-SQL script for the object in question. What you don’t get, at least that I’ve found, is any way to generate scripts for multiple objects.
Everything we’ve looked through so far, except for backups, has been GUI driven stuff. So, where’s the automation for all this? The simple answer is, same place it’s always been. You’ll write T-SQL for the things controlled within the database, just as you’ve always done. You’ll write PowerShell for everything else. That’s it. No real magic as such. However, if you are looking to automate process through PowerShell (which I strongly, STRONGLY, recommend), there’s nothing wrong with a little cheating. Go and get the DBA Tools and use them to make your life a lot easier.
Azure Data Studio does include graphical mechanisms for exporting either data or objects from within the databases on your instances. However, nothing it introduces is actually new or different simply because you’ve always had the capacities that Azure Data Studio exposes. It just shows you different ways to get the export done.