Andras

Software Architect - Red Gate Software

Altering CLR assemblies in SQL Server 2005

Published Wednesday, May 10, 2006 1:39 PM

Category: SQL Server development
SQL Server versions: 2005
Level: Advanced

 

SQL Server 2005 introduced support for .Net in SQL Server. While this is a very powerful feature, it does come with a high cost.

 

The CLR support allows writing stored procedures, functions, triggers using programming languages like C#. It allows creating complicated user defined types, and so forth. But while these new constructs can improve query performance, they can make the lives of developers rather difficult. The problem is with updating the new CLR objects.

 

SQL Server 2005 stores the CLR assemblies in the database itself. Therefore stored procedures and data types that depend on a CLR assembly do not depend on anything else but the database, and are persisted completely in a database backup. Restoring such a backup on a different server will make the CLR objects available in the restored database.

 

But, like stored procedures need to be changed sometimes, CLR object code is evolving too. This means that the assembly containing CLR objects needs to be updated in a database. TSQL contains an “ALTER ASSEMBLY” command to replace a CLR assembly with a new one. Visual Studio 2005 deploys CLR objects exactly this way. However this statement has some restrictions. One of the most important is that method signatures must remain the same. If this is not the case, and a single method signature has changed, the ALTER ASSEMBLY statement will fail. So how can the assembly be upgraded in this case? Simple. One needs to drop all the CLR objects (stored procedures, functions, DDL and DML triggers), drop the assembly, create the assembly using the new DLL, and recreate all the CLR objects. Sounds tedious, but this is still not the end of the story. CLR assemblies may contain user defined types. Rebuilding (i.e. dropping and creating) an assembly requires that such user defined types are dropped before the assembly is dropped. But this is not possible if there are tables that use the user defined data type.

 

So, would dropping the tables that use the CLR user defined types, then dropping the user defined type together with all the relevant CLR stored procedures and other objects, then the assembly be the right way to start an assembly upgrade? Well, if the data in our tables is not important, then the answer is yes. This is rarely the case though.

 

What SQL Compare 5.x does is that it first analyzes the CLR assemblies in the two databases that are compared. It intelligently decides whether a simple ALTER ASSEMBLY statement can be used to modify the assembly, or whether the assembly needs to be rebuilt. In the latter case SQL Compare unbinds the data from tables that depend on a CLR user defined type by migrating the data into a set of temporary tables. These temporary tables use the string representation of the user defined type. Having done this all the relevant CLR objects are dropped, and finally the CLR assembly can be rebuilt. Following this all the above CLR objects are rebuilt based on the new CLR assembly, and the data in the temporary tables is converted back to use the new CLR user defined type. By default we assume that the string representation of a CLR UDT is the same in the two versions. This assumption holds in the majority of the cases. However, in the cases where the string representation changes between the different CLR assembly versions, an extra conversion function can be added manually to the migration script.

 

The nice thing is that all of this is done automatically in SQL Compare 5.x, thus upgrading to a new version of CLR assembly does no longer require potentially dropping all dependent CLR objects by hand and risking data loss.

 

Andras

by András

Comments

No Comments
You need to sign in to comment on this blog

About András

András Belokosztolszki is the architect of SQL Compare versions 4, 5, 6 and 7, SQL Log Rescue and SQL Refactor. He is focused on database internals, database synchronization and database schema evolution.

















<May 2006>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Larry Gonick: Geek of the Week
 Cartoonist, mathematician, historian and environmentalist. Larry Gonick proved that learning could be... Read more...

A SysAdmin's Guide to Change Management
 In the first in a series of monthly articles, ‘Confessions of a Sys Admin’, Matt describes the issues... Read more...

Exchange: Recovery Storage Groups
 It can happen at any time: You get a request, as Admin, from your company, to provide the contents of... Read more...

Build Your Own Virtualized Test Lab
 Desmon explains the fundamentals of building a test lab for Windows servers and Enterprise applications... Read more...

Rendering Hierarchical Data with the Treeview
 It sometimes happens that Web Server controls that visualize data don't quite fit with the way that... Read more...