4.6. DDL Support for CLR IntegrationSQL Server introduces new T-SQL statements to create and manage .NET assemblies and UDTs, and enhances other T-SQL statements to create and manage functions, stored procedures, triggers, and UDA functions created from CLR assemblies. These statements are described in Table 4-4.
The statements are described in detail in the following subsections. Chapter 5 presents in-depth discussions and examples of creating these objects in C#. 4.6.1. CLR Routine MetadataCatalog views return metadata used by the SQL Server 2005 Database Engine. The sys.all_objects catalog view returns a row for each user-defined object and system object in the current database. The type column specifies the object typethe CLR routine type values are shown in Table 4-5.
For example, the following T-SQL statement returns information about all CLR stored procedures in the AdventureWorks database: USE AdventureWorks GO SELECT * FROM sys.all_objects WHERE type='PC' The sys.all_objects catalog view does not return information for DDL triggers. Use the sys.triggers catalog view instead to return information for all DML and DDL triggers in the current database. The sys.all_objects catalog view does not return information for UDTs. The sys.types catalog view returns information for all system and user-defined types in the current database. The sys.assembly_types catalog view returns information for all CLR UDTs. SQL Server 2005 provides catalog views that contain information about registered assemblies and CLR functions, stored procedures, triggers, UDTs, and UDA functions defined from registered assemblies. These catalog views are described in Table 4-6 and detailed in the following subsections.
4.6.1.1. sys.assembliesThe sys.assemblies catalog view contains a row for each assembly registered in the current database. Table 4-7 describes the columns in this view.
The following T-SQL statement returns a result set of all the CLR assemblies registered in the current database: SELECT * FROM sys.assemblies; 4.6.1.2. sys.assembly_filesThe sys.assembly_files catalog view contains a row for each file in each registered assembly in the current database. Table 4-8 describes the columns in this view.
4.6.1.3. sys.assembly_modulesThe sys.assembly_modules catalog view contains a row for each CLR function (scalar-valued, table-valued, and aggregate), stored procedure, or trigger defined in a .NET Framework assembly in the current database. Table 4-9 describes the columns in this view.
4.6.1.4. sys.assembly_referencesThe sys.assembly_references catalog view contains a row for each pair of assemblies registered in the current database where one assembly directly references another. Table 4-10 describes the columns in this view.
4.6.1.5. sys.assembly_typesThe sys.assembly_types catalog view contains a row for each UDT in the current database that is defined in a CLR assembly. Table 4-11 describes the columns in this view.
4.6.2. Assembly ManagementA .NET Framework assembly contains classes and methods that can implement CLR routines in SQL Server 2005. You first have to register the assembly with SQL Server by using the CREATE ASSEMBLY T-SQL statement as you did earlier in the "Hello World Example" section. A registered assembly can be modified using the ALTER ASSEMBLY statement, or removed from the server using the DROP ASSEMBLY statement. These three new T-SQL statements are described in the following subsections. 4.6.2.1. CREATE ASSEMBLYThe CREATE ASSEMBLY T-SQL statement registers a .NET Framework assembly as an object within SQL Server from which CLR stored procedures, UDFs, triggers, UDA functions, and UDTs can be created. The CREATE ASSEMBLY syntax is: CREATE ASSEMBLY assembly_name [ AUTHORIZATION owner_name ] FROM { client_assembly_specifier | assembly_bits [,...n] } [ WITH PERMISSION_SET = { SAFE | EXTERNAL_ACCESS | UNSAFE } ] client_assembly_specifier :: = '[\\computer_name\]share_name\[path\]manifest_file_name' | '[local_path\]manifest_file_name' assembly_bits :: = { varbinary_literal | varbinary_expression } where:
Multiple versions of the same assembly can be uploaded to the server. These assemblies must have different version numbers or cultures and must be registered using unique assembly names within SQL Server. 4.6.2.2. ALTER ASSEMBLYThe ALTER ASSEMBLY T-SQL statement modifies the properties of an assembly previously registered using the CREATE ASSEMBLY statement and refreshes the assembly with the latest version. The ALTER ASSEMBLY syntax is: ALTER ASSEMBLY assembly_name [ FROM { client_assembly_specifier | assembly_bits [ ,...n ] } ] [ WITH assembly_option [ ,...n ] ] [ DROP FILE { file_name [ ,...n ] | ALL } ] [ ADD FILE FROM { client_file_specifier [ AS file_name ] | file_bits AS file_name } [,...n ] ] client_assembly_specifier :: = '\\computer_name\share-name\[path\]manifest_file_name' | '[local_path\]manifest_file_name' assembly_bits :: = { varbinary_literal | varbinary_expression } assembly_option :: = PERMISSION_SET { SAFE | EXTERNAL_ACCESS | UNSAFE } | VISIBILITY { ON | OFF } ] | UNCHECKED DATA where:
Other arguments are the same as defined for the CREATE ASSEMBLY statement. Executing ALTER ASSEMBLY does not affect currently executing sessions running the assembly being alteredthey complete using the unaltered assembly. A new application domain is created running the latest bits for new users of the assembly. If the FROM clause is not specified, the assembly is refreshed with the latest copy of the assembly rebinding CLR routines to the latest implementation in the assembly. 4.6.2.3. DROP ASSEMBLYThe DROP ASSEMBLY statement removes an assembly previously registered with the CREATE ASSEMBLY statement. The assembly and all of its associated files are removed from the database. The DROP ASSEMBLY syntax is: DROP ASSEMBLY assembly_name [ WITH NO DEPENDENTS ] where:
Executing DROP ASSEMBLY does not affect currently executing sessions running the assembly being droppedthey run to completion. New attempts to invoke code in the assembly fail. You cannot drop an assembly that is referenced by another assembly or that is used by a CLR function, stored procedure, trigger, UDT, or UDA function. 4.6.3. User-Defined FunctionsA user-defined function (UDF ) is a routine written by the user that returns either a scalar value (scalar-valued function) or a table (table-valued function). SQL Server 2005 functions can be created directly from T-SQL statements or from methods in a registered .NET Framework assembly. UDFs are created, changed, and removed in SQL Server using the CREATE FUNCTION, ALTER FUNCTION, and DROP FUNCTION T-SQL statements. These statements have been enhanced in SQL Server 2005 to support CLR UDF management. The following subsections describe the enhancements. 4.6.3.1. CREATE FUNCTIONThe CREATE FUNCTION T-SQL statement has been enhanced in SQL Server 2005 to let you create a CLR scalar-valued or table-valued UDF from a UDF implemented as a method in a .NET Framework assembly. You must first register the assembly using the CREATE ASSEMBLY statement. The CREATE FUNCTION syntax for creating CLR UDFs follows: CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [AS] [ type_schema_name. ] scalar_parameter_data_type } [ ,...n ] ] ) RETURNS { scalar_return_data_type | TABLE clr_table_type_definition } [ WITH clr_function_option [ [,] ...n ] ] [ AS ] EXTERNAL NAME method_specifier [ ; ] method_specifier ::= assembly_name.class_name.method_name clr_table_type_definition ::= ( { column_name data_type }[ ,...n ] ) clr_function_option ::= [ RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT ] | [ EXECUTE_AS_Clause ] where:
4.6.3.2. ALTER FUNCTIONThe ALTER FUNCTION T-SQL statement has been enhanced in SQL Server 2005 to let you modify a CLR UDF previously created using the CREATE FUNCTION statement. The ALTER FUNCTION syntax for creating CLR UDFs follows: ALTER FUNCTION [ schema_name. ] function_name ( { @parameter_name [AS] [ type_schema_name. ] scalar_parameter_data_type } [ ,...n ] ) RETURNS { scalar_return_data_type | TABLE <clr_table_type_definition> } [ WITH clr_function_option [ ,...n ] ] [ AS ] EXTERNAL NAME method_specifier method_specifier ::= [ assembly_schema. ] assembly_name.class_name.method_name clr_table_type_definition :: = ( { column_name data_type } [ ,...n ] ) clr_function_option ::= [ RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT ] | [ EXECUTE_AS_Clause ] The arguments are the same as for the CREATE FUNCTION statement discussed in the preceding section. 4.6.3.3. DROP FUNCTIONThe DROP FUNCTION T-SQL statement removes one or more UDFs previously created using the CREATE FUNCTION statement. The SQL Server 2005 DROP FUNCTION statement is the same as in SQL Server 2000. It now supports removing CLR UDFs. The DROP FUNCTION syntax is: DROP FUNCTION { [ schema_name. ] function_name } [ ,...n ] DROP FUNCTION will fail if T-SQL functions or views in the database created with SCHEMABINDING or indexed computed columns reference this function. 4.6.4. Stored ProceduresA stored procedure is a saved collection of T-SQL statements or a reference to a CLR method that optionally takes and returns arguments and returns one or more result sets of data. SQL Server 2005 stored procedures can be created directly from T-SQL statements or from methods in registered .NET Framework assemblies. Stored procedures are created, updated, and removed using the CREATE PROCEDURE, ALTER PROCEDURE, and DROP PROCEDURE T-SQL statements. These statements have been enhanced in SQL Server 2005 to support CLR stored procedure management. The following subsections describe these enhancements. 4.6.4.1. CREATE PROCEDUREThe CREATE PROCEDURE T-SQL statement has been enhanced in SQL Server 2005 to let you create a CLR stored procedure from a stored procedure implemented as a method in a .NET Framework assembly. You must first register the assembly using the CREATE ASSEMBLY statement as you did earlier in the "Hello World Example" section. The enhancements supporting CLR stored procedures are highlighted in the CREATE PROCEDURE syntax that follows: CREATE PROC [ EDURE ] [schema_name.] procedure_name [ { @parameter [ type_schema_name. ] data_type } [ VARYING ] [ = default ] [ [ OUT [ PUT ] ] [ ,...n ] [ WITH <procedure_option> [ ,...n ] [ FOR REPLICATION ] AS { sql_statement [ ...n ] | method_specifier } procedure_option ::= [ ENCRYPTION ] [ RECOMPILE ] [ EXECUTE_AS_Clause ] sql_statement ::= { [ BEGIN ] statements [ END ] } where the new arguments are as follows:
4.6.4.2. ALTER PROCEDUREThe ALTER PROCEDURE T-SQL statement has been enhanced in SQL Server 2005 to let you modify a CLR stored procedure previously created using the CREATE PROCEDURE statement. The enhancements supporting CLR stored procedures are highlighted in the ALTER PROCEDURE syntax that follows: ALTER PROC [ EDURE ] [schema_name.] procedure_name [ { @parameter [ type_schema_name. ] data_type } [ VARYING ] [ = default ] [ [ OUT [ PUT ] ] [ ,...n ] [ WITH procedure_option [ ,...n ] ] [ FOR REPLICATION ] AS { sql_statement [ ...n ] | method_specifier } procedure_option ::= [ ENCRYPTION ] [ RECOMPILE ] [ EXECUTE_AS_Clause ] sql_statement ::= { [ BEGIN ] statements [ END ] } where the new arguments are as follows:
4.6.4.3. DROP PROCEDUREThe DROP PROCEDURE T-SQL statement removes one or more stored procedures previously created using the CREATE PROCEDURE statement. The SQL Server 2005 DROP PROCEDURE statement is the same as in SQL Server 2000. It now supports removing CLR stored procedures. The DROP PROCEDURE syntax is: DROP PROCEDURE { [ schema_name. ] procedure } [ ,...n ] 4.6.4.4. MetadataThe sys.procedures catalog view contains a row for each stored procedure in the current database. The view inherits columns from the sys.objects and sys.all_objects catalog views, so you can limit the rows returned to CLR stored procedures by filtering on the type column, as shown in the following statement: SELECT * FROM sys.procedures WHERE type='PC' The WHERE clause specifying the type PC returns CLR stored procedures. Specifying type P returns SQL stored procedures. 4.6.5. User-Defined Aggregate FunctionsUser-defined aggregate (UDA functions compute a value over a group in a result set. SQL Server 2005 UDA functions can be created directly from T-SQL statements or from a class in a registered .NET Framework assembly. UDA functions are created and removed in SQL Server using the CREATE AGGREGATE and DROP AGGREGATE T-SQL statements. These statements have been enhanced in SQL Server 2005 to support CLR UDA function management. The following subsections describe the enhancements. 4.6.5.1. CREATE AGGREGATEThe CREATE AGGREGATE T-SQL statement creates a UDA function from an implementation by a class in a registered assembly. The assembly must first be registered using the CREATE ASSEMBLY statement. The CREATE AGGREGATE syntax is: CREATE AGGREGATE [ schema_name. ] aggregate_name ..(@parameter input_sqltype ) RETURNS return_sqltype EXTERNAL NAME assembly_name [ .class_name ] input_sqltype ::= ..system_scalar_type | { [ udt_schema_name. ] udt_type_name } return_sqltype ::= ..system_scalar_type | { [ udt_schema_name. ] udt_type_name } where:
4.6.5.2. DROP AGGREGATEThe DROP AGGREGATE T-SQL statement removes a UDA function previously created using the CREATE AGGREGATE statement. The DROP AGGREGATE syntax follows: DROP AGGREGATE [ schema_name. ] aggregate_name where:
The DROP AGGREGATE statement does not execute if there are views, functions, or stored procedures created with schema binding that reference the UDA function. 4.6.6. User-Defined TypesSQL Server 2000 supports user-defined types (UDTs ), also known as alias types. You create these by using the sp_addtypes system stored procedure. They are derived from SQL Server built-in data types and optionally have integrity constraints called rules. SQL Server 2005 extends UDT functionality by letting you define CLR UDTs from a class in a registered .NET Framework assembly. A CLR UDT can store multiple items and expose methods, properties, and attributes. You can use a UDT as the data type for a column in a table, as a T-SQL variable, or as a parameter for stored procedures or functions. UDTs are created and removed in SQL Server using the CREATE TYPE and DROP TYPE T-SQL statements. These statements have been enhanced in SQL Server 2005 to support CLR UDT management. The following subsections describe the enhancements. 4.6.6.1. CREATE TYPEThe CREATE TYPE statement has been enhanced in SQL Server 2005 to let you create a CLR UDT from a type implemented as a class or structure in a .NET Framework assembly. You must first register the assembly using the CREATE ASSEMBLY statement. The enhancements supporting CLR UDTs are highlighted in the CREATE TYPE syntax that follows: CREATE TYPE [ schema_name. ] type_name { FROM base_type [ ( precision [ , scale ] ) ] [ NULL | NOT NULL ] | EXTERNAL NAME assembly_name [ . class_name ] } where:
4.6.6.2. DROP TYPEThe DROP TYPE T-SQL statement removes a UDT previously created using the CREATE TYPE statement. The SQL Server 2005 DROP TYPE statement is the same as in SQL Server 2000. It now supports removing CLR UDTs . The DROP TYPE syntax is: DROP TYPE [ schema_name. ] type_name where:
DROP TYPE will not execute if there are tables in the database with columns of the UDT, if there are columns of the sql_variant data type that contain the UDT, or if there are functions, stored procedures, or triggers in the database created with the WITH SCHEMABINDING clause that use variables or parameters of the UDT. 4.6.6.3. MetadataThe sys.assembly_types catalog view contains a row for all CLR UDTs in the current database. The view inherits all columns from the sys.types catalog view. 4.6.7. TriggersA trigger is a type of stored procedure that executes in response to one or more specific database events. DML triggers execute when data is modified using T-SQL DML statements such as INSERT, UPDATE, or DELETE. DDL triggers execute when database objects are modified using T-SQL DDL statements such as CREATE, ALTER, and DROP. SQL Server 2005 DML and DDL triggers can be created directly from T-SQL statements or from methods in a registered .NET Framework assembly. Triggers are created, changed, and removed using the CREATE TRIGGER, ALTER TRIGGER, and DROP TRIGGER T-SQL statements. These statements have been enhanced in SQL Server 2005 to support CLR trigger management. The following subsections describe the enhancements. 4.6.7.1. CREATE TRIGGERThe CREATE TRIGGER T-SQL statement has been enhanced in SQL Server 2005 to let you create a CLR DML or DDL trigger from a trigger implemented as a method in a .NET Framework assembly. You must first register the assembly using the CREATE ASSEMBLY statement. The enhancements supporting CLR triggers are highlighted in this section. The CREATE TRIGGER syntax for DML triggers is: CREATE TRIGGER [ schema_name. ]trigger_name ON { TABLE | VIEW } [ WITH dml_trigger_option [ ,...n ] ] { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] } [ WITH APPEND ] [ NOT FOR REPLICATION ] AS { sql_statement [ ...n ] | EXTERNAL NAME method specifier } dml_trigger_option ::= [ ENCRYPTION ] [ EXECUTE AS Clause ] The CREATE TRIGGER syntax for DDL triggers is: CREATE TRIGGER trigger_name ON { ALL SERVER | DATABASE } [ WITH <ddl_trigger_option> [,...n ] ] { FOR | AFTER } { event_type | event_group } [ ,...n ] AS { sql_statement [ ...n ] | EXTERNAL NAME <method specifier> } ddl_trigger_option ::= [ ENCRYPTION ] [ EXECUTE AS Clause ] where the new arguments for both DML and DDL triggers are:
4.6.7.2. ALTER TRIGGERThe ALTER TRIGGER T-SQL statement has been enhanced in SQL Server 2005 to let you modify a CLR DML or DDL trigger previously created using the CREATE TRIGGER statement. The enhancements supporting CLR triggers are highlighted in this section. The ALTER TRIGGER syntax for DML triggers follows: ALTER TRIGGER schema_name.trigger_name ON ( TABLE | VIEW ) [ WITH dml_trigger_option [ ...,n ] ] ( FOR | AFTER | INSTEAD OF ) { [ DELETE ] [ , ] [ INSERT ] [ , ] [ UPDATE ] } [ NOT FOR REPLICATION ] AS { sql_statement [ ...n ] | EXTERNAL NAME method specifier } dml_trigger_option ::= [ ENCRYPTION ] [ EXECUTE AS Clause ] The ALTER TRIGGER syntax for DDL triggers follows: ALTER TRIGGER trigger_name ON { DATABASE | ALL SERVER } [ WITH ddl_trigger_option [ ...,n ] ] { FOR | AFTER } { event_type [ ,...n ] | event_group } AS { sql_statement | EXTERNAL NAME method specifier } ddl_trigger_option ::= [ ENCRYPTION ] [ EXECUTE AS Clause ] The new arguments for both DML and DDL triggers are the same as for the CREATE TRIGGER statement discussed in the preceding section. 4.6.7.3. DROP TRIGGERThe DROP TRIGGER T-SQL statement removes one or more DML or DDL triggers previously created using the CREATE TRIGGER statement. The SQL Server 2005 DROP TRIGGER statement is the same as in SQL Server 2000. It now supports removing CLR triggers. The DROP TRIGGER syntax for DML triggers is: DROP TRIGGER schema_name.trigger_name [ ,...n ] The DROP TRIGGER syntax for DDL triggers is: DROP TRIGGER trigger_name [ ,...n ] ON { DATABASE | ALL SERVER } 4.6.7.4. MetadataThe sys.triggers catalog view contains a row for each trigger in the current database. You can limit the rows returned to CLR triggers by filtering on the type column, as shown in the following statement: SELECT * FROM sys.triggers WHERE type='TA' |