Ordina SOFTC Presentation - TFS and JAVA, better together
Ordina SOFTC Presentation - SQL CLR
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
Notas del editor
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