Ordina SOFTC Presentation - SQL CLR

542 views
481 views

Published on

Ordina's Stand out from the Crowd event of May 26th - the presentation, see them all on Slideshare Ordina Belgium

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
542
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Technology for hosting the Microsoft .NET Common Language Runtime engine within SQL Server.Allows managed code to be hosted by and run in the Microsoft SQL Server environment.Alternative for T-SQL and XpsReplace Extended SP’s with a safer alternativeReplace COM automation: sp_OA_*Complex T-SQL algoritmsrecommended replacement for xp_cmdshellT-SQLDevelopers should view the CLR as an efficient alternative for logic that cannot be expressed declaratively in the query languageFor all the other logic, T-SQL will be sufficient.Hosting layer provides coordination of:Assembly LoadingMemory managementSecurity ModelReliabilityThreads & FibersDeadlock detectionExecution context
  • Bigger Programmer productivity.Enhanced programming model: .NET Framework languages are richer than T-SQL ,offering capabilities not available to SQL developers. Enhanced safety and security: Managed code runs in a CLR environment, hosted by the database engine. Allows .NET Framework database objects to be safer and more secure than extended stored procedures.Common development environment:Database development integrated into Visual Studio development environment. same tools available for dev and debugging as in other .NET devs.Performance and scalability:managed code compiles to native code prior to execution, => significant performance increases in some scenarios.
  • Stored procedures (SPs) which are analogous to procedures or void functions in procedural languages like VB or C,triggers which are stored procedures that fire in response to Data Manipulation Language (DML) or Data Definition Language (DDL) events,User-defined functions (UDFs) => scalare and table functions, which are analogous to functions in procedural languages,User-defined aggregates (UDAs) which allow developers to create custom aggregates that act on sets of data instead of one row at a time,User-defined types (UDTs) that allow users to create simple or complex data types which can be serialized and deserialized within the database
  • Function => 1 Scalar => regex function 2 table valuedStored procedure => Read csv files on a shared location and load the data into the database Example: A company has several store locations. Each location has a disconnected ticking system, out of which CSV files with personall ticking times are generated. These files are then posted on an FTP server. \\\\ord01170\\Tickings is the share to the shared locationEmployeeTickings is the table to insert inOur CLR stored procedure will read the files on the shared directory and upload them into the central system in order to calculate the hours worked by personnel. UDT = > currency
  • Be very carfull when using UNSAFE, you can do anything with it, so KNOW YOUR CODE!
  • Short demo showing turning on SQLCLR in SSMSBy default CLR Integration is turned offTurn on via the Surface Area Configuration Manager, orRight-Click Database ServerSelect FacetsSelect Surface Area ConfigurationSet ClrIntegrationEnabled to TrueTransact SQL (T-SQL) Assembly InstallationOnce we have coded our C# assembly, we need to get it installed onto our SQL Server instance.The following steps need to be executed to install your CLR Code into SQL ServerAllow SQL Server to run CLR CodeWrite you code in C# or Vb.netCompile your codeLoad the assembly into SQL ServerCreate the database object and point it to the assemblySince we create our examples in Visual Studio, deployment of CLR assemblies to your database is even made easier. When you have created your code, you can compile and just Right Click and the Select DEPLOY in your project. (I will demonstrate this.)CREATE ASSEMBLY CLRDemo FROM 'C:\\MyAssemblyPath\\CLRDemo.dll'WITH PERMISSION_SET = SAFE;
  • Demo it via T-SQL and via SSMS
  • Like in all other .NET developments, you can set configurations to be used in your assemblies…In sql clr, configuration can always be done like we know it in genuine .NET develepment…. By using a configuration file.However, since we are in SQL Clr, the assembly is uploaded into a SQL Server database, where it is stored in the system catalogs using the CREATE ASSEMBLY data definition language (DDL) statement.This has the nasty side effect that we can only use the configuration file when it is called sqlserver.exe.config!In order to get the configuration even to work for our assemblies, we need to restart the SQL ServerIf you do not want to have your production server rebooted each time you deploy configuration changes, there is a better practice to have your configuration done in a dynamic way. Use a table, which you can call configuration for example and store your keys and values in there. This way, you can add, change configuration keys when necessary, without having to restart your SQL Server.
  • When you decide to look at CLR, you can ask yourself some of the following questions, to help you decide wether or not you should use CLR...Set based: Selecting, filtering, sorting Versioning
  • Ordina SOFTC Presentation - SQL CLR

    1. 1. Where .NET meets SQL Server… SQL CLRJurgen Asselman.NET and SQL Server Consultantjurgen.asselman@ordina.be<br />
    2. 2. About me…<br />Jurgen Asselman<br />14 years of experience in IT<br />@Ordina since 2007<br />Before Ordina +7 years @Sony <br />.NET and SQL Server Consultant @Elia<br />MCIT PRO SQL Server 2008<br />2<br />
    3. 3. Agenda<br />About SQL CLR<br />Benefits<br />What can it be used for?<br />The permissions overview<br />Installation of a CLR assembly<br />The configuration issue…<br />Tips & tricks<br />When to use SQL CLR<br />Conclusion<br />3<br />
    4. 4. About SQL CLR<br />Hosting Microsoft .NET Framework in SQL Server Engine<br />Run managed code in your database<br />Alternative for<br />Extended Stored Procedures<br />COM Automation<br />Complex T-SQL algoritms<br />xp_cmdshell<br />Does NOT replace T-SQL nor DBAs<br />4<br />SQL Engine<br />CLR<br />Hosting<br />Layer<br />SQL OS Layer<br />Windows OS<br />
    5. 5. Benefits<br />Larger programmer productivity<br />Enhanced programming model<br />Enhanced safety and security<br />Common development environment<br />Performance and scalability<br />5<br />
    6. 6. What can it be used for?<br />Stored Procedures (SP)<br />Triggers<br />User-Defined Functions (UDF)<br />User Defined Aggregates (UDA)<br />User Defined Types (UDT)<br />6<br />
    7. 7. Demo time<br />Create a User Defined Function, <br />a Stored Procedure <br />and a User Defined Type<br />7<br />
    8. 8. The permissions overview<br />SAFE<br />Allows data access, basic libraries<br />EXTERNAL_ACCESS<br />Allows access to IO libraries<br />UNSAFE<br />Allows access to unmanaged code<br />8<br />
    9. 9. Installation of a CLR assembly<br />By default SQL CLR is turned off  turn it on<br />Via <br />T-SQL <br />SSMS<br />Install your assemblies via<br />T-SQL<br />SSMS<br />9<br />
    10. 10. Demo time<br />Enable SQL CLR<br />Install our created assembly<br />10<br />
    11. 11. The configuration issue…<br />The .config file as we know it in .NET development<br />in sqlserver.exe.config!<br />change configuration items  restart your SQL Server<br />Best practiceconfiguration table in which keys and values are stored<br />11<br />
    12. 12. Tips & tricks <br />Enable SQL CLR on your database server<br />EXECsp_configure‘clr enabled’, 1<br />RECONFIGURE<br /> GO<br />12<br />
    13. 13. Tips & tricks <br />When you create your CLR UDF or Stored Procedure the T-SQL way, take a look at this syntax<br />CREATE[ FUNCTION / STORED PROCEDURE / TRIGGER … ]({parameters})<br />[RETURNS {return value}]<br />AS<br /> EXTERNAL NAME [Assembly].[Namespace].[Function]<br />13<br />
    14. 14. Tips & tricks <br />Using a connection string in the stored procedure, when connecting to the current database<br />using(SqlConnection connection = newSqlConnection("context connection=true"))<br /> {<br />//your code here<br /> }<br />14<br />
    15. 15. When to use SQL CLR<br />Do I need to manipulate data before it is displayed?<br />.NET code and SQL CLR<br />Do I need to do set-based operations?<br />T-SQL<br />Do I need to do extensive calculations or custom algorithms?<br />.NET code and SQL CLR<br />Are my developers SQL gurus but .NET newbies<br />T-SQL<br />Do I have loads of stored procedures that are hard to manage?<br />.NET code and SQL CLR<br />15<br />
    16. 16. Demo time<br />Use our created and installed assembly <br />in SQL Server<br />16<br />
    17. 17. Conclusion<br />SQL CLR is a powerful feature<br />Advantages<br />Use .NET base-class-library<br />Implement Complex Code<br />Interface with external resources<br />Caution<br />Doesn’t replace T-SQL<br />17<br />
    18. 18. Questions?<br />18<br />
    19. 19. Want to know more? <br />Contact Jurgen Asselman<br />Email: jurgen.asselman@ordina.be<br />Blog: http://jurgenasselman.blogspot.com<br />Twitter: @jurgenasselman<br />19<br />
    20. 20. Follow Ordina…<br />20<br />Share your thoughts via #SOFTC <br />Follow us on twitter: @OrdinaBE<br />Find the presentationsonwww.slideshare.net/ordinaBe<br />Be informed atwww.linkedin.com/company/ordina-belgium<br />

    ×