Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Module 13

Implementing Managed Code in


SQL Server
Module Overview

Introduction to CLR Integration in SQL Server


• Implementing and Publishing CLR Assemblies
Lesson 1: Introduction to CLR Integration in SQL
Server

Options for Extending SQL Server Functionality


Introduction to the .NET Framework
.NET Common Language Runtime
Why Use Managed Code with SQL Server?
Considerations When Using Managed Code
• Appropriate Use of Managed Code
Options for Extending SQL Server Functionality

• With CLR managed code, you can extend the


functionality of SQL Server
• Executes under the management of the .NET Framework
CLR
• Written in Visual C# or Visual Basic
• Use it to create user-defined types, aggregates,
mathematical functions, and other functionality
• SQL Server components such as SSIS, SSAS, and
SSRS are also extensible
• Extended stored procedures are deprecated
• Prone to memory leaks and other performance issues
• Use CLR managed code instead
Introduction to the .NET Framework

• The Win32 and Win64 APIs evolved over time


• They are complex and inconsistent in their design

• The .NET Framework is an object-oriented


development framework
• It is consistent and well-designed
• It includes thousands of class libraries
• It provides a layer of abstraction above the Windows
operating system
• Generally well-regarded by developers

• .NET Framework provides a good basis for


writing code to extend SQL Server functionality
.NET Common Language Runtime

• CLR is the runtime environment for the .NET


Framework—it provides a number of services
including:
• Running code
• Providing services
• Avoiding memory leaks through garbage collection
• Destroys objects that are no longer used
• Operating within other programming environments
• Enabling interoperability between languages through
the common language specification (CLS)
Why Use Managed Code with SQL Server?

• The .NET Framework provides a rich class library


• Managed code can be used to create objects that
you normally create using Transact-SQL:
• User-defined functions (scalar and table-valued)
• Stored procedures
• Triggers (DML and DDL)

• Managed code can be used to create new types


of objects:
• User-defined data types
• User-defined aggregates
Considerations When Using Managed Code

• Considerations when using managed code:


• Portability—upgrading your database
• Maintainability—consider expertise required to maintain:
• The database
• Managed code components
• Consider using a three-tier architecture
• Database tier
• Mid tier
• Presentation tier
• Transact-SQL is well suited to working with SQL Server
tables and data
• Use managed code sparingly; otherwise consider a three-
tier architecture
Appropriate Use of Managed Code
Lesson 2: Implementing and Publishing CLR
Assemblies

What Is an Assembly?
Assembly Permission Sets
SQL Server Data Tools
Publishing a CLR Assembly
• Demonstration: Creating a User-Defined Function
What Is an Assembly?

• Managed code is deployed in SQL Server using


an assembly
• A SQL Server assembly is:
• A specially structured .dll file
• Self-describing through its manifest

• Assemblies contain compiled executable


managed code
• They might also contain other recourses
• Use SQL Server Data Tools (SSDT) to create
managed code, and to publish it to SQL Server
Assembly Permission Sets

• Sp_configure has an option called CLR strict


security
• Improves the security of CLR assemblies
• CLR Strict Security set to 1 by default
• Can be set to 0 to disable, but not recommended

• CLR Strict Security overrides


• PERMISSION_SET = SAFE
• PERMISSION_SET = EXTERNAL_ACCESS
• Both treated as UNSAFE

• Add trusted assemblies to a white list


• Sys.sp_add_trusted_assembly
SQL Server Data Tools

• SQL Server Development Tools (SSDT) was


introduced with SQL Server 2012
• Integrates with Visual Studio
• Familiar development environment
• Use SSDT to develop and deploy CLR assemblies

• SSDT templates for:


• Aggregates
• Stored procedures
• Triggers
• User-defined functions
• User-defined types

• Permission level is SAFE by default


Publishing a CLR Assembly

• Visual Studio and SSDT is a familiar environment


for .NET software developers
• SSDT contains a number of SQL CLR templates:
• Aggregate
• Stored procedure
• User-defined function
• User-defined type

• Set properties for the assembly


• Permission level
• Target platform

• Build the assembly and publish to your database


Demonstration: Creating a User-Defined
Function

In this demonstration, you will see how to:


• Develop a simple function using CLR C#
managed code
• Publish an assembly
Lab: Implementing Managed Code in SQL Server

Exercise 1: Assessing Proposed CLR Code


Exercise 2: Creating a Scalar-Valued CLR Function
• Exercise 3: Creating a Table-Valued CLR Function

Logon Information
Virtual machine: 20762C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd

Estimated Time: 45 minutes


Lab Scenario

You work for the rapidly expanding Adventure


Works Bicycle Company Inc. A new developer has
joined the database team and has decided to
implement almost all of the logic in SQL CLR
assemblies. You will determine if this is
appropriate. You will also implement and test a
supplied .NET assembly.
Lab Review

• After publishing managed code to a database,


what do you think the issues are with using it?
Module Review and Takeaways

• Review Question(s)

You might also like