26
An Introduction To An Introduction To CLR Integration in CLR Integration in SQL Server 2005 SQL Server 2005 (Yukon) (Yukon) Dr Greg Low Dr Greg Low

An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Embed Size (px)

Citation preview

Page 1: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

An Introduction To An Introduction To CLR Integration in CLR Integration in SQL Server 2005 SQL Server 2005 (Yukon)(Yukon)

Dr Greg LowDr Greg Low

Page 2: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Who Am I?Who Am I?

Director of White Bear ConsultingDirector of White Bear Consulting

Director of Lowell ComputingDirector of Lowell Computing

Microsoft MVP for .NETMicrosoft MVP for .NET

INETA User Group Relations Chair for Asia-PacificINETA User Group Relations Chair for Asia-Pacific

President of Qld MSDN User GroupPresident of Qld MSDN User Group

President of Qld SQL Server User GroupPresident of Qld SQL Server User Group

Page 3: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

What we will cover:What we will cover:

Why have CLR Integration? (Is T-SQL Why have CLR Integration? (Is T-SQL Dead?)Dead?)Dealing with AssembliesDealing with AssembliesExample AssembliesExample Assemblies

Scalar-Valued FunctionsScalar-Valued FunctionsTable-Valued FunctionsTable-Valued FunctionsStored ProceduresStored ProceduresTriggersTriggersUser-defined Data TypesUser-defined Data TypesUser-defined AggregatesUser-defined Aggregates

Management IssuesManagement IssuesConclusionConclusion

Page 4: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Versions UsedVersions UsedThis material is based on the Beta 1 This material is based on the Beta 1 Refresh of Whidbey (September CTP) Refresh of Whidbey (September CTP) and Beta 2 of SQL Server 2005 (Yukon).and Beta 2 of SQL Server 2005 (Yukon).

Source material for the CLR examples is Source material for the CLR examples is summarised and updated from the summarised and updated from the MSDN article:MSDN article:

Overview of .NET Programming Features in Overview of .NET Programming Features in SQL Server "Yukon" Beta 1SQL Server "Yukon" Beta 1

http://msdn.microsoft.com/library/http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/default.asp?url=/library/en-us/dnsql90/html/sql_ovyukonnetprogfeatures.asphtml/sql_ovyukonnetprogfeatures.asp

Page 5: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Is T-SQL Dead?Is T-SQL Dead?

T-SQL remains the language of choice T-SQL remains the language of choice for data-intensive operationsfor data-intensive operations

But, there are many things it’s not great But, there are many things it’s not great at doing…at doing…

Splitting Strings T-SQL StyleSplitting Strings T-SQL Style

Splitting Strings VB StyleSplitting Strings VB Style

Check ConstraintCheck Constraint

Validating Email Address FormatValidating Email Address Format

Page 6: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

T-SQL vs Managed CodeT-SQL vs Managed Code

T-SQL best for data access with minimal T-SQL best for data access with minimal procedural logicprocedural logic

Managed code best for CPU intensive Managed code best for CPU intensive operations or complex logic (or operations or complex logic (or accessing base class library)accessing base class library)

Decision is needed regarding client vs Decision is needed regarding client vs server regarding use of processing server regarding use of processing power.power.

Page 7: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

CLR Integration Basics CLR Integration Basics

CLR integrated into the databaseCLR integrated into the database

SQL Server acts as the host for the CLRSQL Server acts as the host for the CLR

Any .NET language ok (VB & C# most Any .NET language ok (VB & C# most common)common)

Stored Procedures, Triggers & User-Stored Procedures, Triggers & User-Defined Functions can now be in Defined Functions can now be in assembliesassemblies

Two new object types: Two new object types: User-defined type (no longer just a sub-User-defined type (no longer just a sub-type) type)

User-defined aggregateUser-defined aggregate

Page 8: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Why Have CLR Integration?Why Have CLR Integration?

Why CLR?Why CLR?Type safety, garbage collection, rich class Type safety, garbage collection, rich class library, exception handling, thread library, exception handling, thread managementmanagementLoaded on first execution of a .NET Loaded on first execution of a .NET assemblyassembly

User-defined types (classes) and User-defined types (classes) and aggregatesaggregatesCommon IDE environmentCommon IDE environmentHigher performance in some situationsHigher performance in some situationsRich programming modelRich programming model

Arrays, collections, loops, encapsulation, Arrays, collections, loops, encapsulation, inheritance, polymorphism, etc.inheritance, polymorphism, etc.

.NET code access security model.NET code access security model

Page 9: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Why CLR Integration Why CLR Integration (continued)(continued)

Namespaces help organise code assetsNamespaces help organise code assets

Managed code better for number-Managed code better for number-crunching tasks (plus string handling, crunching tasks (plus string handling, regular expressions, file access, regular expressions, file access, cryptography, etc.)cryptography, etc.)

Classes that don't make sense (eg Classes that don't make sense (eg WinForms) are not availableWinForms) are not available

Page 10: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Dealing With AssembliesDealing With Assemblies

What is an assembly?What is an assembly?

Only DLLs can be registeredOnly DLLs can be registered

Register via CREATE ASSEMBLY Register via CREATE ASSEMBLY statement:statement:CREATE ASSEMBLY MyCLR FROM 'C:\MyApp\CREATE ASSEMBLY MyCLR FROM 'C:\MyApp\MyCLR.DLL'MyCLR.DLL'

Unregister via DROP ASSEMBLY Unregister via DROP ASSEMBLY statement:statement:

DROP ASSEMBLY MyCLRDROP ASSEMBLY MyCLR

Page 11: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Dealing With Assemblies Dealing With Assemblies (cont)(cont)

Data and code (ie assemblies) owned Data and code (ie assemblies) owned by a user are isolated from those owned by a user are isolated from those owned by another user unless access is by another user unless access is granted.granted.

Permission can be given to create an Permission can be given to create an assembly.assembly.

The owner can then assign permission The owner can then assign permission to reference the assembly.to reference the assembly.

Calling chains (assembly calls Calling chains (assembly calls assembly) can be an issue. Must be in assembly) can be an issue. Must be in same database and with appropriate same database and with appropriate permissions.permissions.

Page 12: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Dealing With Assemblies Dealing With Assemblies (cont)(cont)

3 security levels for assemblies (can be 3 security levels for assemblies (can be specified during CREATE as specified during CREATE as WITH WITH PERMISSION_SET =PERMISSION_SET = SAFE|EXTERNAL SAFE|EXTERNAL ACCESS|UNSAFE)ACCESS|UNSAFE)

Default is Default is SAFESAFE

EXTERNAL ACCESSEXTERNAL ACCESS allows network, allows network, registry, file system, environment registry, file system, environment variablesvariables

UNSAFEUNSAFE is unrestricted (eg Win32 API) is unrestricted (eg Win32 API)

Page 13: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Example AssembliesExample Assemblies

Scalar-valued User-defined Functions, Scalar-valued User-defined Functions, Table-valued User-defined Functions, Table-valued User-defined Functions, User-Defined Procedures and User-User-Defined Procedures and User-defined Triggersdefined Triggers

Inproc Provider is contained in Inproc Provider is contained in System.Data.SqlServerSystem.Data.SqlServer (similar to (similar to System.Data.SqlClient but optimised for System.Data.SqlClient but optimised for working with data inside the SQL Server working with data inside the SQL Server process)process)

Requires a reference to Requires a reference to sqlaccess.dllsqlaccess.dll

3 new objects: 3 new objects: SqlContextSqlContext, , SqlPipeSqlPipe and and SqlTriggerContextSqlTriggerContext

Page 14: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Scalar-Valued Functions DemoScalar-Valued Functions Demo

Notes:Notes:DataAccessKind.None allows higher DataAccessKind.None allows higher optimisationoptimisationMicrosoft.VisualStudio.DataTools.SqlAttrMicrosoft.VisualStudio.DataTools.SqlAttributes reference is required for previous ibutes reference is required for previous betabetaT-SQL assembly name must match the T-SQL assembly name must match the dll name (maybe…)dll name (maybe…)T-SQL function name does not have to T-SQL function name does not have to match the target method's namematch the target method's nameVB namespace makes the syntax more VB namespace makes the syntax more challengingchallenging

Page 15: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Further Function ExampleFurther Function Example

Much more reasonable use of CLR Much more reasonable use of CLR integrationintegration

Consider what would be involved in T-Consider what would be involved in T-SQL to accomplish the sameSQL to accomplish the same

Page 16: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Table-Valued FunctionsTable-Valued Functions

UDF that returns a tableUDF that returns a table

Data is returned through an Data is returned through an ISqlReaderISqlReader

Declarations are similar to ScalarDeclarations are similar to Scalar

CREATE FUNCTION SomeFunction CREATE FUNCTION SomeFunction RETURNS @SomeTable TABLE (AColumn RETURNS @SomeTable TABLE (AColumn INT ,AnotherColumn VARCHAR(35)) INT ,AnotherColumn VARCHAR(35)) EXTERNAL NAME EXTERNAL NAME SomeAssembly.SomeNamespaceOrClassSomeAssembly.SomeNamespaceOrClass.SomeFunction.SomeFunction

Page 17: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Stored ProceduresStored Procedures

Can return tabular results and Can return tabular results and messages, invoke DDL and DML and messages, invoke DDL and DML and return parameters.return parameters.

SqlPipeSqlPipe object is used to return tabular object is used to return tabular results and messagesresults and messages

SqlPipe has an overloaded SqlPipe has an overloaded Send()Send() methodmethodSqlPipe sp = SqlContect.GetPipe();SqlPipe sp = SqlContect.GetPipe();

SqlDataReader SomeReader = SqlDataReader SomeReader = cmSQL.ExecuteReader();cmSQL.ExecuteReader();

sp.Send(SomeReader);sp.Send(SomeReader);

Page 18: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Stored Procedures (cont)Stored Procedures (cont)

[SQLProcedure][SQLProcedure] attribute on method attribute on method

Register by:Register by:

CREATE PROCEDURE MyProc AS EXTERNAL CREATE PROCEDURE MyProc AS EXTERNAL NAME NAME MyAssembly.MyNamespaceOrClass.MyPMyAssembly.MyNamespaceOrClass.MyProcroc

Page 19: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

TriggersTriggers

Similar to previous code using SqlPipe Similar to previous code using SqlPipe but access to but access to INSERTEDINSERTED and and DELETEDDELETED tables as in T-SQL triggers.tables as in T-SQL triggers.

Dim TriggerContext As SqlTriggerContext = Dim TriggerContext As SqlTriggerContext = SqlContext.GetTriggerContext()SqlContext.GetTriggerContext()

Dim TriggerPipe As SqlPipe = Dim TriggerPipe As SqlPipe = SqlContext.GetPipe()SqlContext.GetPipe()

If TriggerContext.TriggerAction = If TriggerContext.TriggerAction = TriggerAction.Insert ThenTriggerAction.Insert Then

……

Register via:Register via:CREATE TRIGGER MyTrigger ON MyTable FOR INSERT CREATE TRIGGER MyTrigger ON MyTable FOR INSERT

AS EXTERNAL NAME AS EXTERNAL NAME SomeAssembly.SomeNamespace.MyTriggerSomeAssembly.SomeNamespace.MyTrigger

Page 20: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

User-Defined Data TypesUser-Defined Data Types

Now possible to build a CLR-based class Now possible to build a CLR-based class and use it in a CREATE TYPE statement.and use it in a CREATE TYPE statement.

Rules apply:Rules apply:Must be SerializableMust be Serializable

Must Have SqlUserDefinedTypeAttributeMust Have SqlUserDefinedTypeAttribute

Should implement INullable (ie should be Should implement INullable (ie should be NULL aware)NULL aware)

Must have a public constructor with no Must have a public constructor with no argumentsarguments

Should support conversion to/from a string Should support conversion to/from a string via ToString() and Parse()via ToString() and Parse()

Page 21: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

User-Defined Types ExampleUser-Defined Types Example

SqlUserDefinedTypeAttribute SqlUserDefinedTypeAttribute properties:properties:

MaxByteSize (maximum size in bytes)MaxByteSize (maximum size in bytes)

IsFixedLength (are all instances the same IsFixedLength (are all instances the same length)length)

IsByteOrdered (can binary representation IsByteOrdered (can binary representation be used in comparisons)be used in comparisons)

Format (Native, UserDefined, Format (Native, UserDefined, SerializedDataWithMetaData)SerializedDataWithMetaData)

Page 22: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

User-Defined Aggregate User-Defined Aggregate ExampleExample

Totally new object type for SQL ServerTotally new object type for SQL Server

Four methods required:Four methods required:Init()Init()

Accumulate()Accumulate()

Merge()Merge()

Terminate()Terminate()

SqlUserDefinedAggregateAttributeSqlUserDefinedAggregateAttributeIsInvariantToDuplicates IsInvariantToDuplicates (MAX & MIN vs (MAX & MIN vs SUM)SUM)

IsInvariantToNulls IsInvariantToNulls (MIN & SUM vs COUNT)(MIN & SUM vs COUNT)

IsInvariantToOrderIsInvariantToOrder

Page 23: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Management & Design IssuesManagement & Design Issues

How does an assembly get to the How does an assembly get to the server? (file system vs streaming)server? (file system vs streaming)

CLR versioning issues (& publishing CLR versioning issues (& publishing policy)policy)

Normalisation ????Normalisation ????

Page 24: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

ConclusionsConclusions

Stunning new set of capabilitiesStunning new set of capabilities

Many DBA's very scared…Many DBA's very scared…

Lots of design decisions to be Lots of design decisions to be consideredconsidered

Page 25: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

For More InformationFor More Information

SQL Server DBA’s Guide To The .NET SQL Server DBA’s Guide To The .NET Framework And CLR IntegrationFramework And CLR Integration

(coming soon). Send an email to (coming soon). Send an email to [email protected]@whitebearconsulting.com for for prerelease infoprerelease info

Overview of .NET Programming Overview of .NET Programming Features in SQL Server "Yukon" Beta 1Features in SQL Server "Yukon" Beta 1

MSDN LibraryMSDN Library

http://msdn.microsoft.com/library/http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql90/default.asp?url=/library/en-us/dnsql90/html/sql_ovyukonnetprogfeatures.asphtml/sql_ovyukonnetprogfeatures.asp

Page 26: An Introduction To CLR Integration in SQL Server 2005 (Yukon) Dr Greg Low

Thanks for listening!Thanks for listening!

[email protected]@[email protected]@lowell.com.au