4. Migrating Data into SQL Azure
There are many ways to get data into a SQL Azure database. You could run a script that contains a bunch of INSERT
statements or explore some other ways, including these:
- The bulk copy utility (
BCP.exe
): BCP is a
command-line tool that has been around for a long time. Out of the box,
it can upload data to a SQL Azure database. - SQL Server Integration Services (SSIS): SSSIS is a powerful
tool for creating simple and complex dataflows. To SSIS SQL Azure is
just another datasource. Keep in mind that, since OLE DB is not
supported with SQL Azure, to use SSIS, you will have to use the ADO.NET
library.
- SQL Azure Migration wizard: CodePlex is an open source
community that provides free tools to the public based on the Microsoft
platform. One of the projects on CodePlex is the SQL Azure Migration
Wizard. This wizard will help you migrate SQL Server 2005/2008 databases
to SQL Azure. Not only will the wizard move the database, it has the
ability to analyze trace files against Azure. This is very important for
application compatibility testing.
One of my favorite databases is pubs
. It’s still available bundled with Northwind and available as a free download at http://archive.msdn.microsoft.com/northwind
. After downloading this zip file, simply connect to a local SQL Server instance and run the INSTPUBS.SQL
script against your server, and you will have pubs
and party like its 1999 all over again.
To get pubs
in SQL Azure, we could
have executed the script directly against our SQL Azure database, but we
would need to make a slight modification to the script to remove the USE
statements. Changing database contexts via an existing connection to
SQL Azure is not something you can do. You would need to break the
connection and reconnect to the other database to perform this action in
SQL Azure. For purposes of demonstration, let’s assume the pubs
database is installed in an on-premise SQL Server. When we launch SQLAzureMW.exe
, we are presented with the wizard screen shown in Figure 8.
Figure 8. SQLAzureMW Select Process page
To migrate our PUBS
database, click
the SQL Database button, and click Next. At this point, we will be
asked to connect to the source SQL Server that has the database we want
to migrate. Provide the necessary connection information, and select
“pubs” from the “Select source” page in the wizard. The next page will
allow us to select which objects we want to migrate over. By default,
this wizard will migrate all database objects including stored
procedures, tables, user defined data types, and views. After this page,
the wizard will confirm that you are ready to start generating the SQL
Script. While the wizard is generating the script, you will see the
status of the migration. This is the first step in the migration, and
the wizard is essentially scripting the objects and using BCP.exe
to push out the data into a file on the local directory. The Results Summary page is displayed in Figure 9.
Figure 9. Results Summary page for the first step in migration
Next, the wizard will ask you for the location
of the SQL Azure database. Remember that “User name” in this case is not
just the administrative user name we defined but includes “@servername”
at the end of the username, as shown in Figure 10.
Figure 10. Connection dialog
After connecting to the SQL Azure server, you will be asked to which database to add pubs
. Since we do not have pubs
, we can click the Create Database button on the Setup Target Server Connection panel and create a new database called pubs
.
The next page will execute the script and BCP of the original data
against the new SQL Azure database. Results of this are shown in Figure 11.
Figure 11. Target Server Response dialog
Now, we have a copy of pubs
living in our SQL Azure database!
5. Understanding SQL Azure specific views
A few system views are only available on a SQL
Azure database. If you connect to your SQL Azure Server and enumerate
the system views within the master
database, you will see
these. You can notice these special SQL Azure–only ones by a small lock
on the bottom right side of the view icon.
To return information about the bandwidth used by each database, we can use the sys.bandwidth_usage
view. When we copy the pubs
database to SQL Azure, we see exactly how much bandwidth was consumed
since that operation. This view returns two rows for every database with
one row showing data ingress and the other showing data egress. The
results for a SELECT * FROM sys.bandwidth_usage
is shown in Table 1.
The quantity
field is in kilobytes (KB).
The sys.database_usage
returns one
row for each day of your subscription. The view returns the type of
database (i.e., business or web) and the maximum number of databases of
that SKU type that existed for that day. This view is useful to give you
a guesstimate of the cost per day of the SQL Azure database.
The sys.firewall_rules
view returns
the list of firewall rules associated with the SQL Azure database.
The sys.dm_db_objects_impacted_on_version_change
view is a database-scoped view that provides early warning for objects
that will be impacted by version upgrades. Version upgrades happen, and
with SQL Azure, you need to stay current. Even though Microsoft does
everything possible to remain backward compatible, there is some chance
that things may break if not tested.
6. Backing Up and Restoring Data with SQL Azure
At the time of this writing there is no way to
easily backup and restore your database within SQL Azure. You may be
curious as to why such a fundamental maintenance task is missing from
the standard capabilities of SQL Azure and the answer lies in the fact
that a SQL Azure database inherently is highly available. Multiple
copies of your database are created automatically as soon as you create
the database. These multiple copies protect you from loss of the entire
database. Server failures and failover happen automatically for SQL
Azure databases and require no interaction or configuration on your
part.
However, the story is still incomplete. What if
someone calls you and says, “I accidently dropped a table can you
restore a backup”? This is where there is no good answer as of yet. Your
database as a whole is protected, but there is no good way to restore a
single object such as an inadvertently dropped table.
Right now, you can use SSIS or an SQLCMD script
to automate the data creation or regeneration. You may also look into
creating a database copy. Of course, you would need to make a copy
before this disaster happens. To create a database copy, simple execute
the following command: CREATE DATABASE Pub2 AS COPY OF Pubs
.
Note that this will create a separate database of the same size as your
original database. If you are counting your pennies, maintaining a
separate database may add up in cost and will certainly add up in
storage space if your databases are large.
There is system view called sys.dm_database_copies
.
This view will show information on the status of the copy operation.
For relatively small databases, there won’t be a lot of useful
information here, because the copy would happen so quickly the percentage_complete
data would be meaningless. If the database failed to copy, the error_state
column would reflect this.
7. Storing More Than Relational Data
The first effort with SQL Azure is to develop a
highly scalable and robust relational database engine that can work with
your Windows Azure applications and with on-premise data. Microsoft is
not stopping at relational data. At the time of this writing, the SQL
Azure Reporting feature is in Community Technology Preview
(otherwise known as beta release). The first iteration of SQL Azure
Reporting Services will include the ability to create and execute
reports based off of data stored in Windows Azure or a SQL Azure
database. Although the first version won’t be at feature parity with the
on-premise SQL Server Reporting Services, it’s a step in the business
intelligence direction. Although there is no official word on details
regarding Microsoft’s story for SQL Server Analysis Services in the
cloud, you can bet there is something in the works.