Share via


Stretch DB – The Programmatic Approach

Stretch Database “stretchs” on premise data to the cloud. This simple pattern and approach allows for ready access to cold data, no changes to queries while keeping data secure on premise, in the cloud and even during migration.

There have been many excellent blogs already about Stretch Database, some of them here and here. In the newer versions of SQL Management Studio (SSMS), there are many options on how to administer the Stretch functionality. The purpose of this blog post is to provide additional detail around how to enable/disable Stretch in a programmatic manner that is better suited towards the operational side of the house in enterprise scenarios.

Figure 1 shows the various operations options that are available to a “stretched” table. stretchpic1

Figure 1 : Options in Management Studio for stretch operations

 

If for any operational purpose, a user may want to disable stretch and leave the data in Azure while certain operations are happening on site (remember from the links in the above blogs that Stretch when enabled will auto synchronize any changes via metadata) and they do not want to use the Management Studio interface, they can use the following T-SQL Command (from MSDN):

 USE <Stretch-enabled database name>;
GO
ALTER TABLE <Stretch-enabled table name>
   SET ( REMOTE_DATA_ARCHIVE = OFF_WITHOUT_DATA_RECOVERY ( MIGRATION_STATE = PAUSED ) ) ;
GO

 

Note that, the remote Azure SQL table will not be deleted. The remote table must be dropped via Azure Management Portal or other supported means such as Power Shell.

On our server, it will look like the following:

stretchpic2
Figure 2: Disable Stretch while leaving data in Azure

 

Checking the GUI, we can see that the Stretch is indeed disabled:

stretchpic3

Figure 3- Stretch disabled in SQL Server Management Studio

To re-enable Stretch at anytime, simply issue the following command:

 USE <Stretch-enabled database name>;
GO
ALTER TABLE <Stretch-enabled table name>  
   SET ( REMOTE_DATA_ARCHIVE ( MIGRATION_STATE = OUTBOUND ) ) ;
GO

Entering this command yields:

stretchpic4

Figure 4- Resuming Stretch

We can see that it is indeed resumed:

stretchpic5

Figure 5: Verifying in SSMS

 

Note that the above example performed two actions: Disable + Leave data in Azure. If I wished to simply “pause” the stretch process I could have used this command:

 USE <Stretch-enabled database name>;
GO
ALTER TABLE <Stretch-enabled table name>
   SET ( REMOTE_DATA_ARCHIVE ( MIGRATION_STATE = PAUSED ) ) ;
GO

As shown above, Stretch capabilities are not merely controlled via the SQL Server Management Studio but via Transact-SQL to better fit into the world of operations and automation. This allows for this great new feature to live and support enterprise development/operations as required by the industry.