Migrating to a Contained Database in SQL Server 2012

Migrating to a Contained Database in SQL Server 2012

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0
A few days ago, an article of mine on Contained Databases in SQL Server 2012 was published on the MVP Award Program Blog.

In that article I explained with an example how you can easily create and access a partially contained database from scratch.

This is the second part of the article that explains how you can convert a “normal” database to a partially contained database thus making it fully portable.

A typical security/user access configuration for a database would be set up as follows: Create the login (SQL or Windows) under the SQL Server Instance “Security” module, and then set the proper “User Mapping” along with the corresponding database role membership(s).

This is illustrated in the following screenshot where we see the user access configuration for the sample database “NormalDB”:

However, what would you do if you needed to migrate the database to another instance? If you did not migrate the SQL login as well, you would encounter the issue of orphaned database users (not associated to a SQL login).

With partially contained databases there is not such an issue for the simple reason that you do not need to have a SQL login associated to the database user, you just need the database user! 🙂

So, let’s see how we can convert “NormalDB” to a partially contained database.

First of all, we need to enable “contained database authentication” on the SQL Server instance if not already enabled:

USE [master]
GO


sp_configure ‘contained database authentication’, 1
GO
RECONFIGURE
GO

Then, we change the containment option for the database to “PARTIAL“:

ALTER DATABASE [NormalDB]
SET CONTAINMENT=PARTIAL
GO

And here’s the last step where the magic takes place:

USE [NormalDB]
GO


sp_migrate_user_to_contained 
@username = N’NormalDBLogin’,
@rename = N’keep_name’,
@disablelogin = N’disable_login’
GO

sp_migrate_user_to_contained is a special stored procedure shipped with SQL Server 2012 and its purpose is to remove dependencies between a database and the SQL Server instance that hosts it. More specifically, it separates the user from the original SQL Server login.

In the above example, what sp_migrate_user_to_contained did was to set the login’s password to the contained database user and then remove the SQL Server login.

So, it’s time to access that contained database!

Set the database to connect to (NormalDB):



Enter the contained database’s user credentials:























Access granted!

I hope you enjoyed the post!

Cheers!


Recommended eBooks on SQL Server:

Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Tuning SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Administering SQL Server: eBook by SQL Server MVP Artemakis Artemiou
Artemakis Artemiou
Artemakis Artemiou is a Senior SQL Server Architect, Author, Software Developer and a Microsoft Data Platform MVP. He has over 15 years of experience in the IT industry in various roles. Among other, via his initiative SQLEBooks.com, Artemakis authors and publishes eBooks on different topics on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Additionally he is the founder of the SQLArtBits initiative that aims to provide the technical community with simple, yet powerful and high-quality SQL Server tools. Currently, the highlights of these tools are DBA Security Advisor and In-Memory OLTP Simulator. Artemakis's official website can be found at aartemiou.com. Artemakis's blogs can be found at: SQLNetHub.com and TechHowTos.com.