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
Add a Comment