sqlclr programming with sql server 2005 - by andrew novick

65
© 2006 Andrew Novick SQLCLR Programming with SQL Server 2005 Andrew Novick SQLCLR Programming with SQL Server 2005  Andrew Novick

Upload: kevin-tan

Post on 09-Apr-2018

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 1/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

SQLCLR Programming

with SQL Server 2005

 Andrew Novick

Page 2: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 2/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

The Subject:

What is SQLCLR programming?

Why would you use it?

Why not to use it?

Page 3: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 3/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

Introduction ± Andrew Novick

Novick Software

 ± Business Application Development

 ± SQL Server and .Net specialization

www.NovickSoftware.com

Books:

 ± Transact-SQL UDFs

 ± SQL 2000 XML Distilled

Page 4: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 4/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

 Authorship

This presentation is based in part on material developed for the

Mini Code-Camp ³Programming SQL Server: From 2000 to

2005´ By Andrew Novick and Adam Machanic

 Adam¶s Web site is:

http://www.datamanipulation.net

 Andrew Novick¶s Web Site is:

http://www.NovickSoftware.com

Page 5: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 5/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

What Is SQLCLR?

Writing code using .Net Languages that r unsinside SQL Server.

 ± User-Defined Functions

 ± Stored Procedures ± Aggregates

 ± User-Defined Types

 ± Triggers

 ± SSIS Packages

 ± Expressions and Data Providers in Reports

Page 6: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 6/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

Why Use .Net in S2K5

Replace Extended SP¶s with a safer alternative

Replace COM automation: sp_ O A_*

Take advantage of .Net Framework classes

Run computationally intense algorithms Reuse code

Programmer productivity

Use new capabilities not available inT

-SQL ± User Defined Types

 ± User Defined Aggregates

Page 7: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 7/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Hosting the CLR

S2K5 is one of many CLR hosts

 ± Such as: ASP.Net, WinForms

Goals for CLR hosting: ± Security

 ± Reliability

 ± Performance

Page 8: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 8/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

SQLCLR Overall Architecture

Runtime hosted within SQL Server¶s

process space

³OS´ layer provided by SQL Server¶s

scheduler, SQLOS ± Memory

 ± Processor 

 ± IO Hosting enabled on a per-server basis

Page 9: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 9/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

SQLCLR AppDomains

App Domains created per database, per assembly owner 

SQL Server will restart an app domain if itdetects:

 ± Deadlocks

 ± Memory leaks

 ± Hung threads

 ± « And other abnormal conditions

Page 10: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 10/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CLR is Optional!

Disabled by default

You must turn it on to use it

 ± SQL Server 2005 Surface Area Configuration

 ± This script:

EXEC sp_configure 'show advanced options' , '1';

goreconfigure;

goEXEC sp_configure 'clr enabled' , '1'

goreconfigure;

Page 11: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 11/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

 Assemblies

.Net Code compiled into an IL DLL

Assemblies must be added to S2K5 with

CREATE ASSEMBLY

Bits are stored in the database¶s

sys.assembly_files table

CREATE ASSEMBLY my_assembly_name

FROM µ\\myserver\«path\MyAssembly.dll¶

Page 12: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 12/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

 Assemblies 2

Viewing Assemblies

 ± SQL Management Studio

 ± Ref lector with the SQL 2005 Browser 

Page 13: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 13/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

 Assembly Security: PERMISSION_ SET

S AFE

 ± Allows data access, basic libraries

EXTERNAL _ACCESS

 ± Allows access to IO libraries

UNS AFE

 ± Allows access to unmanaged code

Page 14: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 14/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

PERMISSION_ SET: Safe

May not access external resources:registry, file system, or network

May not use unmanaged code or PInvoke

May access data using the current contextbut not via SQLClient or any other data

provider 

No thread processing

Page 15: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 15/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

PERMISSION_ SET: EXTERNAL _ACCESS

May access external resources:registry, file system, network, environment

variables

May not use:

 ± unmanaged code

 ± PInvoke

Page 16: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 16/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

PERMISSION_ SET: UNS AFE

Can do anything«

Know your code!

Page 17: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 17/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

SQLCLR 

User-Defined Functions

Page 18: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 18/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Overview of CLR UDFs

Two Types ± Scalar 

 ± Table-Valued

Similar Rules

 ± No side effects

More f  lexible ± Can use the .Net Base Classes

 ± Can use user created classes

 ± Can execute Dynamic SQL

Performance ± Similar to better than T-SQL

Page 19: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 19/65

Page 20: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 20/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CLR Scalar Functions

Simple and Flexible

.Net Functions

Usef  ul for encapsulating BCL f unctionality

Page 21: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 21/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CLR Table-Valued Functions

³Streaming´: Returns one row at time

Requires a method with:

 ± Returns IEnumerable

 ± SqlFunctionAttribute

A second method ³shreds´ the data

 ± FillRowMethod ± Columns defined by output parameters

Page 22: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 22/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CLR Table-Valued Function Lifecycle

SQL Server invokes IEnumerable method

Which returns a reference to the collection

SQL Server enumerates the collection

Enumerate() calls the FillRowMethod

 ± Input object is mapped to output parameters

by the

Page 23: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 23/65

Page 24: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 24/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

SQLCLR Stored Procedures

Can do everything that T-SQL sprocs can

 ± Multiple result sets

 ± Output Parms

 ± Messages ± Return Code

Can do what other SQLCLR objects can: ± C# or VB.net code

Page 25: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 25/65

Page 26: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 26/65

Page 27: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 27/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

The Same Rules Apply..!

ADO.NET in-process provider makes iteasy to move code between tiers

Best practice in the application tier:

 ± Use T-SQL stored procedures to encapsulatedatabase data access

Best practice in the data tier (CLR stored

procedure or UDF): ± Use T-SQL stored procedures to encapsulate

database data access

Page 28: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 28/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

 ADO.NET: Sending Back Data

SqlPipe

 ± Send()

Result Sets (SqlDataReader)

Rows (SqlDataRecord) Messages (System.String)

 ± ExecuteAndSend()

Executes SqlCommand in the connected context Should probably be called SendAndExecute()

Page 29: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 29/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CLR Stored Procedures

Best for: ± External Data Sources

 ± Multiple output parameters from external datasources

 ± complex math/logic

Not indicated for 

 ± single result sets ± single scalar output

Page 30: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 30/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

SQLCLR

User Defined Aggregates

Page 31: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 31/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

User Defined Aggregates

Aggregates scalar values into another scalar 

Uses the SqlUserDefinedAggregate attribute

CREATE AGGREGATE [Product](@Value float)

Returns [float]EXTERNAL NAME

[SampleAggregate].[SampleAggregate.Product]

go

GRANT EXEC ON dbo.Product TO PUBLICgo

Page 32: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 32/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Possible UDAGGs

Product

CountOfNulls

BitwiseOr 

Concatenation

RegExMatchCount

Page 33: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 33/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Impossible UDAGG

First

Last

Impossible due to possible Parallelism

Page 34: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 34/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

User Defined Aggregate Class

public class myAggregate

{

public void Accumulate (<input-type> value){ }

public <return-type> Terminate() { }

public void Init () { }

public void Merge (myAggregate) { }

}

Page 35: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 35/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

UDAGG: Init

Public Sub Init()

m_Accumulator = 1.0m_HasNonNull = False

End Sub

Initialize the aggregate

Reinitializes the when SQL Server 

chooses to reuse it.

Page 36: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 36/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

UDAGG: Accumulate

Public Sub Accumulate(ByVal value As SqlDouble)if value.isnull then exit sub

m_Accumulator *= value

m_HasNonNull = True

End Sub

Called once per row to be aggregated.

Page 37: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 37/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

UDAGG: Merge

SQL Server may use multiple threads for an aggregation.

Each thread would need it¶s own instance

of the UDAGG class. Results combined by the Merge method.

Page 38: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 38/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

UDAGG: Merge

Public Sub Merge(ByVal value As Product)

if value is nothing then exit sub

if not value.m_HasNonNull then exit subm_Accumulator *= value.m_Accumulator

m_HasNonNull = True

End Sub

Page 39: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 39/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

UDAGG: Terminate

Public Function Terminate() As SqlDouble

if not m_HasNonNull then

return sqldouble.nullelse

Return m_Accumulatorend if

End Function

Terminate method returns the results.

Page 40: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 40/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Using the User Defined Aggregate

SELECT dbo.product(sample) [Three integers]

FROM ( SELECT CAST(1.0 as float) as SampleUNION ALL SELECT 3

UNION ALL SELECT 5) Numbers

(Result)

Three integers

---------------

15

Page 41: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 41/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Best Practices: UDAGG

Consider T-SQL alternatives

Use SQLTypes

Code efficiently

No SQL input if you can help it.

If you need data, create a cache.

Remember Parallel execution

Page 42: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 42/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

User-DefinedT

ypes

UDT

Page 43: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 43/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Two Types of UDTs

Alias Types

CLR Types

Both use the CREATE TYPE statement

Page 44: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 44/65

Page 45: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 45/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CLR User-Defined Types (UDTs)

Created with the .Net CLR in VB.Net or C#

New to SQL Server 2005

Example:

CREATE Assembly myAssembly FROM

µ\\server\share\myAssembly.dll

Go

CREATE TYPE myType

EXTERNAL NAME myAssembly.[myAssembly.myType]

go

Page 46: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 46/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Why CLR User-Defined Types

Encapsulate application logic and data inthe database.

Benefits: ± Programmer productivity

 ± Consistency over the long term

Page 47: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 47/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Steps to create a CLR UDT

 ± Create a Class in a .Net language (VB or C#)

 ± Create the ASSEMBLY database object

 ± CREATE TYPE statement

Page 48: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 48/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CL ASS or Str ucture

Class is a reference type

Str ucture is a Value type

Page 49: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 49/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Create your CL ASS

Required attributes:<Serializable()> _<Microsoft.SqlServer.Server.SqlUserDefinedType(Format

.Native)>

Required Interface: INullable

Required Methods:

ToStringParse

Page 50: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 50/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Your Class

Public and Private fields

Static (Shared in VB) and instance members

Page 51: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 51/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

SqlUserDefinedType Attribute

Format: Native, UserDefined

IsByteOrdered

IsFixedLength

MaxByteSize

Name

ValidationMethodName

Page 52: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 52/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

 Assemblies

.Net Code compiled into an IL DLL

Assemblies must be added to S2K5 with

CREATE ASSEMBLY

Bits are stored in the database¶ssys.assembly_files table

CREATE ASSEMBLY my_assembly_name

FROM µ\\myserver\«path\MyAssembly.dll¶

Page 53: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 53/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

CREATE TYPE statement

CREATE TYPE [ schema_name.] type_name

{

FROM base_type

[ ( precision [ , scale ] ) ][ NULL | NOT NULL ]

| EXTERNAL NAME assembly_name

[ .class_name ]

} [ ; ]

Page 54: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 54/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Type Name Rules

Standard Rules for identifiers.

Recommendation: Keep It Simple

Page 55: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 55/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

TYPE Permissions

References

Execute

These apply to:

 ± Assembly

 ± Type

Page 56: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 56/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Using Types in SQL

CREATE T ABLE Office (id int, location GEOPOINT)

SELECT id, Location. LatitudeLocation.Longitude

FROM Office

Page 57: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 57/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Type Considerations

.Net value types don¶t represent NULL

System.Data.SQLTypes represent NULL

Use the SQLTypes when possible

Page 58: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 58/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

User Defined Types

Best used for scalar types

Not ideally suitable for ³Classes´

Page 59: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 59/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Best Practices - UDTs

UDTs should represent scalar objects

Separate assembly to each group of UDTs

Test extensively before deployment

Keep caref ul track of the DLL¶s

 ± Use Source Control for them.

Page 60: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 60/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Summary

SQLCLR is a powerf ul new feature Advantages

 ± Use .Net base-class-library

 ± Implement Complex Code ± Interface with external resources

Cautions:

 ± Doesn¶t relpace T-SQL well

Page 61: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 61/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

 Andy¶s Books

Page 62: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 62/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Resources

Newsletter about SQL Coding

T

heme for Fall 2005 is.Net CLR Programming

http://www.novicksoftware.com/coding-in-sql/coding-in-sql-signup.htm

Page 63: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 63/65

© 2006 Andrew NovickSQLCLR Programming with SQL Server 2005Andrew Novick

Download Slides and Examples

Download this presentation and samplesfrom:

http://www.novicksoftware.com/Presentations.htm

New England Visual Basic

Page 64: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 64/65

© 2006 Andrew Novick

SQLCLR Programming with SQL Server 2005Andrew Novick

New England Visual Basic

Professionals

Focused on VB.Net development

Meetings @ MS Waltham

 ± 1st Thursday - 6:15 to 8:30

Coming up: ± Febr uary ± AviCode ± ASP.Net monitoring

 ± March ± Adam Machanic ADO.Net 2.0

 ± April ± Jesse Liberty

Page 65: SQLCLR Programming With SQL Server 2005 - By Andrew Novick

8/8/2019 SQLCLR Programming With SQL Server 2005 - By Andrew Novick

http://slidepdf.com/reader/full/sqlclr-programming-with-sql-server-2005-by-andrew-novick 65/65

SQLCLR Programming with SQL Server 2005

Thanks for Coming

 Andrew Novick

[email protected]

http://www.NovickSoftware.com

978-440-8126