|
There are a number of reasons why you might decide to create a CLR User Defined Type in SQL Server. Some types of data are composite, and the definition of a single component alone isn't very useful. For example, a coordinate in space can have a x, y,and z coordinate that defines the actual location. Having the x without the y and z does not define the location sufficiently. User Defined Types are very good for modeling this kind of data in the database as a single point of datum. However, there are serious implications to the use of a CLR User Defined Type, that must be accounted for when building the type and then deploying it for use.
The first of these considerations is that a User Defined Type once used, can not be updated or redeployed to SQL Server easily. For this reason, extra care must be taken when planning and building a CLR UDT. This is also a strong reason for creating separate Assemblies for any CLR UDT. If a CLR UDT is included in an assembly with other CLR Objects, and you have to add objects or change an object that already exists in the Assembly, you will receive an error when you try to deploy the changed assembly into SQL from Visual Studio, that it can not drop the UDT because it is already in use. Keeping CLR UDT's in their own specific Assembly will keep this problem from happening.
The second consideration follows closely on the first one, and that is once implemented and used, you are limited in your ability to update a CLR UDT's definition without dropping all references to the UDT, to include columns holding data. For this reason, careful care must be taken when designing a UDT to ensure that it covers all of the necessary information. To change the UDT's signature or attributes once it exists, you would have to create a new UDT with the updated signature and add it to SQL. Since SQL Server won't allow you to explicitly convert a CLR UDT to another CLR UDT, you have to manually write the conversion of the type to update table definitions to use the new type definitions.
The third consideration is that User Defined Types are limited to 8000 bytes when serialized in SQL Server 2005. This is found in the User-Defined Type Requirements in the BOL. This limitation has been removed in SQL Server 2008 as of the time of this articles publication so this consideration may not apply to your specific implementation. This limitation is for the serialized representation of the UDT, and follows the same rules as those for the varchar, nvarchar, varbinary, and sql_variant datatypes. More information on this can be found in the BOL article Row-Overflow Data Exceeding 8 KB.
While CLR UDT's offer a way to expand upon the datatypes inherent to SQL Server, extreme care should be taken in implementing them effectively, otherwise you will experience undue headache later.
|