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