/  12
 
Programming SQL Server "Yukon"
Part I - .NET Integration
 Jurgen PostelmansU2U nv/saMarch 3, 2004
Applies to:
SQL Server "Yukon" (beta 1)
Visual Studio "Whidbey" (PDC preview version)
Summary:
This article will give you an overview how you can write stored procedures, triggers and user-defined functions in SQL Server "Yukon" using C# as a programming language.
Contents:
Introduction
One of the major new features of SQL Server "Yukon" is the integration of the .NET FrameworkCommon Language Runtime (CLR) into the SQL Server database engine. This means that you nowhave the possibility to write your stored procedures, triggers and user-defined functions in managedcode. The languages currently supported are C#, Visual Basic .NET, managed C++ and JavaScript.NET. Furthermore, SQL Server "Yukon" can only host the "Whidbey" version of the .NETFramework. Previous versions of the .NET Framework are not supported. For performance reasons,the .NET runtime is lazy loaded by SQL Server. This means that the .NET runtime will only be loadedwhen it is really necessary, such as when you would execute a managed stored procedure for thefirst time.Whether or not you should implement your stored procedures and user-defined functions using .NETcode depends largely on what you do in those functions. If they contain a lot of procedural codethen writing them in .NET will typically make them faster and easier to implement. If, on the otherhand, you do a lot of data access in your functions, implementing them in T-SQL will typically yieldthe best performance.
Starting from a .NET Assembly
To start we will create a new .NET Class Library in Visual Studio "Whidbey". This Class Libraryproject is called MathTutor and will implement some simple mathematical functions.
 
Not all functions that you implement in .NET classes can be accessed by T-SQL. The methods youwant to expose as stored procedures, triggers or user-defined functions must follow at least 3conditions:
They must be in public classes.
They must be static and public.
They cannot be in nested classes.In the MathTutor project we will implement one class called Math with 3 simple functions. The initialversion of this class is shown below.
namespace MathTutor{public class Math{public static SqlInt32 AddNumbers(SqlInt32 i, SqlInt32 j){return i + j;} public static int SubtractNumbers(int i, int j){return i - j;}public static SqlInt32 IncrementBy(SqlInt32 by, ref SqlInt32number){int retValue = 0; try{number += by;}catch (Exception ex){retValue = 1;}return retValue;}}}
Note that to declare the parameters of the methods, you can either use the standard types providedby the .NET Framework or their corresponding SqlTypes. If you know that your methods will only beused in T-SQL it is preferable to use the SqlTypes which are defined in the System.Data.SqlTypesnamespace. The SqlTypes behave in the same way as the built-in SQL Server data types, especiallywhen you're working with NULL values.
 
Registering Class Libraries in SQL Server
Once the Class Library is compiled we need to perform two steps in SQL Server "Yukon" to exposethe methods in our MathTutor class library:
Register the assembly in SQL Server "Yukon".
Register the methods in the assembly as stored procedures, triggers or user-definedfunctions.These steps are mandatory because SQL Server "Yukon" cannot execute any arbitrary managedcode that you might have in your assemblies.The first step is accomplished using the CREATE ASSEMBLY statement as shown below.
CREATE ASSEMBLY MathTutorFROM 'C:\Articles\Yukon\MathTutor\bin\Debug\MathTutor.dll'
The CREATE ASSEMBLY statement registers and loads an assembly in SQL Server. In the FROMclause you specify the location of the assembly you want to register. If this assembly depends onother assemblies you will also need to register these. Once the assembly is registered the originalassemblies on the file system are not used anymore. All the registration information is stored in 3SQL Server system tables. These are called sys.assemblies, sys.assembly_files andsys.assembly_modules.When you load an assembly in SQL Server you have the possibility to specify a security level for youmanaged code. This is done using the WITH PERMISSION_SET parameter as shown below.
CREATE ASSEMBLY MathTutorFROM 'C:\Articles\Yukon\MathTutor\bin\Debug\MathTutor.dll'WITH PERMISSION_SET = SAFE
When the permission set is set to safe, which is the default, the managed code in our assemblycannot access any external resources like the file system, registry, network...To un-register an assembly you can use the DROP ASSEMBLY statement.
DROP ASSEMBLY MathTutor
Registering User-Defined Functions in SQL Server
Once the assembly is registered, AddNumbers and SubtractNumbers can be registered as user-defined functions in T-SQL. This is done using the CREATE FUNCTION statement.
CREATE FUNCTION AddNumbers (@I INT, @J INT) RETURNS INTAS EXTERNAL NAME MathTutor:[MathTutor.Math]::AddNumbersCREATE FUNCTION SubtractNumbers (@I INT, @J INT) RETURNS INTAS EXTERNAL NAME MathTutor:[MathTutor.Math]::SubtractNumbers

Share & Embed

More from this user

Add a Comment

Characters: ...