28
Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08

Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08

Embed Size (px)

Citation preview

Programming the CLR in SQL Server 2005

Mark Blomsma

Develop-One

Session Code: DB.08

• Introductions• The End of T-SQL• How does it work?

– Hosting Layer, SQL OS

• How do I use it?– Stored procedures, Functions, Triggers, Custom aggregations,

User Defined Type

• Deployment• Monitoring• Questions

Programming the CLR in SQL Server 2005

Mark Blomsma

• Professional developer since 1992

• Microsoft Certified Professional

• Microsoft Most Valuable Professional (MVP) for three years running

• Current employment– Software Architect : Develop-One– Consultant : Omnext.NET– Sales Manager North America : MRA Group

The end of T-SQL?

• No! Managed code will not solve all your problems.

– Select/Insert/Update/Deleteare here to stay!

– CLR is an alternative to the procedural portion of T-SQL

So why put C# in the database?

• Complex string manipulation• Encryption• XML manipulation• No more need for external procedures• Tooling support for managed languages• Leverage .NET FCL & third party libraries• For everything that you cannot do with T-SQL

Show me the magic

• Stability is everything for a database• CLR 2.0 offers more control to host environment through

“HostProtection” attribute – No thread creation– No listening on sockets– No finalizers– Allow/deny allocation of memory– Deny killing the host process– CLR is sandboxed through CAS

• Checked during assembly creation process

• CLR 2.0 AppDomain is hosted in SQL Server– Loading and unloading of assemblies is controlled via SQL

Server

Limited FCL support

• Not available– Environment.Exit()– System.Console– System.Windows.Forms– System.Drawing– System.Web– …

SQL OS

• CLR uses SQL OS for:– Memory– Threads/fibers– Synchronization

SQL EngineSQL Engine

WindowsWindows

SQL OSSQL OS

CLRCLR

HostingHostingLayerLayer

Hosting layer

• Hosting layer provides coordination of:– Assembly Loading– Memory management– Security Model– Reliability– Threads & Fibers– Deadlock detection– Execution context

SQL EngineSQL Engine

WindowsWindows

SQL OSSQL OS

CLRCLR

HostingHostingLayerLayer

How do I use it?

• Stored Procedures

• Functions

• Triggers

• Custom Aggregations

• SqlContext– SqlExecutionContext– SqlTriggerContext

Stored procedures

• Public class

• Pubic method

• Method must be static

• Use [SqlProcedure] attribute

• Use in, out inout and return parameters

Stored procedures

Functions

• Public class

• Public method

• Method must be static

• Use [SqlFunction] attribute

• Must return a value

Functions

Triggers

• Public class

• Public method

• Method must be static

• Use [SqlTrigger] attribute– [SqlTrigger(Event=“FOR INSERT”)]

• Use SqlTriggerContext for getting at the data related to the trigger.

Triggers

Custom Aggregations

• Public struct

• Use [SqlUserDefinedAggregate] attribute

• Implement:– Init– Accumulate– Merge– Terminate

Custom Aggregations

User Defined Type

• Public struct

• Use [SqlUserDefinedType] attribute

• 8KB size limit.

• Do not use for business objects.

Deployment

• Use sp_configure ‘clr enabled’, ‘1’to enable sqlclr– Off by default

• Use create assembly

• SqlClr uses 3 CAS permission buckets– Safe– External access– Unsafe

Deployment

Monitor assemblies

• Profiler trace events: – CLR:load assembly monitors assembly load requests (successes and failures) – SQL:BatchStarting, BatchCompleted – SP:Starting, Completed, StmtStarting, StmtCompleted monitor execution of

Transact-SQL and CLR routines • Performance counters:

– SQL Server: Total CLR time – .NET CLR Memory – Processor

• DMVs and catalog views: – sys.assembly* shows basic information about the assemblies stored – sys.dm_os_memory_clerks – sys.dm_clr* – sys.dm_exec_query_stats – sys.dm_exec_requests – sys.dm_exec_cached_plans

Monitoring

Tips

• Database server CPU is valuable

• SQLCLR is about making DB programming easier

• Managed code has a performance advantage over T-SQL with respect to most procedural computation, but for data-access T-SQL generally fares better.

Questions?

References

• SQL Server 2005 distilled by Eric Brown, ISBN: 0321349792

• http://msdn.microsoft.com/sql/learning/prog/clr/default.aspx?pull=/library/en-us/dnsql90/html/sqlclrguidance.asp

• http://blog.develop-one.com• This presentation on:

– www.sdn.nl – www.develop-one.com

AddendumNew SQL Server 2005 features

• Presentation by Dinesh Chandrasekhar

• www.microsoft.com/sql/prodinfo/overview/whats-new-in-sqlserver2005.mspx

• http://msdn2.microsoft.com/en-us/library/ms170363(SQL.90).aspx

Adobe Acrobat 7.0 Document

Evaluatieformulier

Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!!

Session Code: DB.08