Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Programming the CLR in  SQL Server 2005 Mark Blomsma Develop-One Session Code:   DB.08
<ul><li>Introductions </li></ul><ul><li>The End of T-SQL </li></ul><ul><li>How does it work? </li></ul><ul><ul><li>Hosting...
Mark Blomsma <ul><li>Professional developer since 1992 </li></ul><ul><li>Microsoft Certified Professional </li></ul><ul><l...
The end of T-SQL? <ul><li>No! Managed code will not solve all your problems.  </li></ul><ul><ul><li>Select/Insert/Update/D...
So why put C# in the database? <ul><li>Complex string manipulation </li></ul><ul><li>Encryption </li></ul><ul><li>XML mani...
Show me the magic <ul><li>Stability is everything for a database </li></ul><ul><li>CLR 2.0 offers more control to host env...
Limited FCL support <ul><li>Not available </li></ul><ul><ul><li>Environment.Exit() </li></ul></ul><ul><ul><li>System.Conso...
SQL OS <ul><li>CLR uses SQL OS for: </li></ul><ul><ul><li>Memory </li></ul></ul><ul><ul><li>Threads/fibers </li></ul></ul>...
Hosting layer <ul><li>Hosting layer provides coordination of: </li></ul><ul><ul><li>Assembly Loading </li></ul></ul><ul><u...
How do I use it? <ul><li>Stored Procedures </li></ul><ul><li>Functions </li></ul><ul><li>Triggers </li></ul><ul><li>Custom...
Stored procedures <ul><li>Public class </li></ul><ul><li>Pubic method </li></ul><ul><li>Method must be static </li></ul><u...
Stored procedures
Functions <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>U...
Functions
Triggers <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>Us...
Triggers
Custom Aggregations <ul><li>Public struct </li></ul><ul><li>Use  [SqlUserDefinedAggregate]  attribute </li></ul><ul><li>Im...
Custom Aggregations
User Defined Type <ul><li>Public struct </li></ul><ul><li>Use  [SqlUserDefinedType]  attribute </li></ul><ul><li>8KB size ...
Deployment <ul><li>Use sp_configure ‘clr enabled’, ‘1’ to enable sqlclr </li></ul><ul><ul><li>Off by default </li></ul></u...
Deployment
Monitor assemblies <ul><li>Profiler trace events:  </li></ul><ul><ul><li>CLR:load assembly monitors assembly load requests...
Monitoring
Tips <ul><li>Database server CPU is valuable </li></ul><ul><li>SQLCLR is about making DB programming easier </li></ul><ul>...
Questions?
References <ul><li>SQL Server 2005 distilled  by Eric Brown, ISBN: 0321349792 </li></ul><ul><li>http://msdn.microsoft.com/...
Addendum New SQL Server 2005 features <ul><li>Presentation by Dinesh Chandrasekhar </li></ul><ul><li>www.microsoft.com/sql...
Evaluatieformulier Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Session Code:   DB.08
Upcoming SlideShare
Loading in …5
×

SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)

1,240 views

Published on

  • Be the first to comment

  • Be the first to like this

SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)

  1. 1. Programming the CLR in SQL Server 2005 Mark Blomsma Develop-One Session Code: DB.08
  2. 2. <ul><li>Introductions </li></ul><ul><li>The End of T-SQL </li></ul><ul><li>How does it work? </li></ul><ul><ul><li>Hosting Layer, SQL OS </li></ul></ul><ul><li>How do I use it? </li></ul><ul><ul><li>Stored procedures, Functions, Triggers, Custom aggregations, User Defined Type </li></ul></ul><ul><li>Deployment </li></ul><ul><li>Monitoring </li></ul><ul><li>Questions </li></ul>Programming the CLR in SQL Server 2005
  3. 3. Mark Blomsma <ul><li>Professional developer since 1992 </li></ul><ul><li>Microsoft Certified Professional </li></ul><ul><li>Microsoft Most Valuable Professional (MVP) for three years running </li></ul><ul><li>Current employment </li></ul><ul><ul><li>Software Architect : Develop-One </li></ul></ul><ul><ul><li>Consultant : Omnext.NET </li></ul></ul><ul><ul><li>Sales Manager North America : MRA Group </li></ul></ul>
  4. 4. The end of T-SQL? <ul><li>No! Managed code will not solve all your problems. </li></ul><ul><ul><li>Select/Insert/Update/Delete are here to stay ! </li></ul></ul><ul><ul><li>CLR is an alternative to the procedural portion of T-SQL </li></ul></ul>
  5. 5. So why put C# in the database? <ul><li>Complex string manipulation </li></ul><ul><li>Encryption </li></ul><ul><li>XML manipulation </li></ul><ul><li>No more need for external procedures </li></ul><ul><li>Tooling support for managed languages </li></ul><ul><li>Leverage .NET FCL & third party libraries </li></ul><ul><li>For everything that you cannot do with T-SQL </li></ul>
  6. 6. Show me the magic <ul><li>Stability is everything for a database </li></ul><ul><li>CLR 2.0 offers more control to host environment through “HostProtection” attribute </li></ul><ul><ul><li>No thread creation </li></ul></ul><ul><ul><li>No listening on sockets </li></ul></ul><ul><ul><li>No finalizers </li></ul></ul><ul><ul><li>Allow/deny allocation of memory </li></ul></ul><ul><ul><li>Deny killing the host process </li></ul></ul><ul><ul><li>CLR is sandboxed through CAS </li></ul></ul><ul><ul><ul><li>Checked during assembly creation process </li></ul></ul></ul><ul><li>CLR 2.0 AppDomain is hosted in SQL Server </li></ul><ul><ul><li>Loading and unloading of assemblies is controlled via SQL Server </li></ul></ul>
  7. 7. Limited FCL support <ul><li>Not available </li></ul><ul><ul><li>Environment.Exit() </li></ul></ul><ul><ul><li>System.Console </li></ul></ul><ul><ul><li>System.Windows.Forms </li></ul></ul><ul><ul><li>System.Drawing </li></ul></ul><ul><ul><li>System.Web </li></ul></ul><ul><ul><li>… </li></ul></ul>
  8. 8. SQL OS <ul><li>CLR uses SQL OS for: </li></ul><ul><ul><li>Memory </li></ul></ul><ul><ul><li>Threads/fibers </li></ul></ul><ul><ul><li>Synchronization </li></ul></ul>SQL Engine Windows SQL OS CLR Hosting Layer
  9. 9. Hosting layer <ul><li>Hosting layer provides coordination of: </li></ul><ul><ul><li>Assembly Loading </li></ul></ul><ul><ul><li>Memory management </li></ul></ul><ul><ul><li>Security Model </li></ul></ul><ul><ul><li>Reliability </li></ul></ul><ul><ul><li>Threads & Fibers </li></ul></ul><ul><ul><li>Deadlock detection </li></ul></ul><ul><ul><li>Execution context </li></ul></ul>SQL Engine Windows SQL OS CLR Hosting Layer
  10. 10. How do I use it? <ul><li>Stored Procedures </li></ul><ul><li>Functions </li></ul><ul><li>Triggers </li></ul><ul><li>Custom Aggregations </li></ul><ul><li>SqlContext </li></ul><ul><ul><li>SqlExecutionContext </li></ul></ul><ul><ul><li>SqlTriggerContext </li></ul></ul>
  11. 11. Stored procedures <ul><li>Public class </li></ul><ul><li>Pubic method </li></ul><ul><li>Method must be static </li></ul><ul><li>Use [SqlProcedure] attribute </li></ul><ul><li>Use in, out inout and return parameters </li></ul>
  12. 12. Stored procedures
  13. 13. Functions <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>Use [SqlFunction] attribute </li></ul><ul><li>Must return a value </li></ul>
  14. 14. Functions
  15. 15. Triggers <ul><li>Public class </li></ul><ul><li>Public method </li></ul><ul><li>Method must be static </li></ul><ul><li>Use [SqlTrigger] attribute </li></ul><ul><ul><li>[SqlTrigger(Event=“FOR INSERT”)] </li></ul></ul><ul><li>Use SqlTriggerContext for getting at the data related to the trigger. </li></ul>
  16. 16. Triggers
  17. 17. Custom Aggregations <ul><li>Public struct </li></ul><ul><li>Use [SqlUserDefinedAggregate] attribute </li></ul><ul><li>Implement: </li></ul><ul><ul><li>Init </li></ul></ul><ul><ul><li>Accumulate </li></ul></ul><ul><ul><li>Merge </li></ul></ul><ul><ul><li>Terminate </li></ul></ul>
  18. 18. Custom Aggregations
  19. 19. User Defined Type <ul><li>Public struct </li></ul><ul><li>Use [SqlUserDefinedType] attribute </li></ul><ul><li>8KB size limit. </li></ul><ul><li>Do not use for business objects. </li></ul>
  20. 20. Deployment <ul><li>Use sp_configure ‘clr enabled’, ‘1’ to enable sqlclr </li></ul><ul><ul><li>Off by default </li></ul></ul><ul><li>Use create assembly </li></ul><ul><li>SqlClr uses 3 CAS permission buckets </li></ul><ul><ul><li>Safe </li></ul></ul><ul><ul><li>External access </li></ul></ul><ul><ul><li>Unsafe </li></ul></ul>
  21. 21. Deployment
  22. 22. Monitor assemblies <ul><li>Profiler trace events: </li></ul><ul><ul><li>CLR:load assembly monitors assembly load requests (successes and failures) </li></ul></ul><ul><ul><li>SQL:BatchStarting, BatchCompleted </li></ul></ul><ul><ul><li>SP:Starting, Completed, StmtStarting, StmtCompleted monitor execution of Transact-SQL and CLR routines </li></ul></ul><ul><li>Performance counters: </li></ul><ul><ul><li>SQL Server: Total CLR time </li></ul></ul><ul><ul><li>.NET CLR Memory </li></ul></ul><ul><ul><li>Processor </li></ul></ul><ul><li>DMVs and catalog views: </li></ul><ul><ul><li>sys.assembly* shows basic information about the assemblies stored </li></ul></ul><ul><ul><li>sys.dm_os_memory_clerks </li></ul></ul><ul><ul><li>sys.dm_clr* </li></ul></ul><ul><ul><li>sys.dm_exec_query_stats </li></ul></ul><ul><ul><li>sys.dm_exec_requests </li></ul></ul><ul><ul><li>sys.dm_exec_cached_plans </li></ul></ul>
  23. 23. Monitoring
  24. 24. Tips <ul><li>Database server CPU is valuable </li></ul><ul><li>SQLCLR is about making DB programming easier </li></ul><ul><li>Managed code has a performance advantage over T-SQL with respect to most procedural computation, but for data-access T-SQL generally fares better. </li></ul>
  25. 25. Questions?
  26. 26. References <ul><li>SQL Server 2005 distilled by Eric Brown, ISBN: 0321349792 </li></ul><ul><li>http://msdn.microsoft.com/sql/learning/prog/clr/default.aspx?pull=/library/en-us/dnsql90/html/sqlclrguidance.asp </li></ul><ul><li>http://blog.develop-one.com </li></ul><ul><li>This presentation on: </li></ul><ul><ul><li>www.sdn.nl </li></ul></ul><ul><ul><li>www.develop-one.com </li></ul></ul>
  27. 27. Addendum New SQL Server 2005 features <ul><li>Presentation by Dinesh Chandrasekhar </li></ul><ul><li>www.microsoft.com/sql/prodinfo/overview/whats-new-in-sqlserver2005.mspx </li></ul><ul><li>http://msdn2.microsoft.com/en-us/library/ms170363(SQL.90).aspx </li></ul>
  28. 28. Evaluatieformulier Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Session Code: DB.08

×