SlideShare a Scribd company logo
1 of 14
Download to read offline
SQL Server™ .NET
                           Programmability


                         José A. Blakeley
                         Architect
                         SQL Server




                        Background
                       l     Transact SQL (T -SQL)
                             Ø   SQL Server’s database programming language
                             Ø   Includes:
                                 §   SQL DDL, SQL DML
                                 §   Variables, assignment, iteration, procedures, functions
                       l     .NET Platform
                             Ø   .NET runtime (Common Language Runtime)
                                 §   Verifiable Intermediate Language
                                 §   Managed memory – garbage collection
                                 §   Multiple languages (e.g, C#, C++, Cobol)
                                                         (e.g ,
                             Ø   Frameworks library (e.g., GUI, Files, XML)
                             Ø   Assemblies – New Dynamic Link Libraries
                                 §   Contain code + metadata (class definitions, assembly
                                     dependencies)
                                 §   Unit of code deployment, security, versioning




PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                             1
Outline
                        l   Motivation
                        l   Basic Infrastructure
                            Ø   Hosting overview
                            Ø   Data access inside process: ADO.NET
                            Ø   SQL types
                        l   SQL features
                            Ø   Server assemblies
                            Ø   Functions, procedures, triggers
                            Ø   Types and aggregates
                        l   Summary




                        Motivation
                        l   Broaden the set of languages for running
                            data-
                            data -intensive business logic in server
                            Ø   IT Developers and ISVs
                            Ø   Multiple languages: Visual Basic ®, C#, C++, J#, …
                            Ø   Same mid - and server -tier data access model
                                      mid-     server-
                            Ø   Same tools: IDE, project model, debugging, …
                            Ø   Transact-
                                Transact-SQL still supported
                        l   Packaged as .NET assemblies
                            Ø   Verifiable, secure
                        l   Deployed to SQL Server as
                            Ø   Functions, procedures, triggers, types
                        l   Basis for SQL Server Extensibility

PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                   2
Development Steps
                                     VB, C#, C++               Build
                                                       VS                 Assembly:
                                                     Project             “TaxLib.dll”
                                                                          TaxLib.dll”

                                  Runtime
                                hosted inside                     SQL Data Definition:
                                                                  create assembly …
                                 SQL Server                       create function …
                                                                  create procedure …
                                                                  create trigger …
                                                                  create type …
                        SQL Queries:                 SQL Server
                        select
                        sum(tax(sal,state)
                        sum(tax(sal,state) )
                        from Emp where county
                        = ‘King’




                        Basic Infrastructure
                        l   Hosting common language runtime
                            inside SQL Server
                            Ø   4Ss: Safety, security, scalability, speed
                            Ø   Run verified, type-safe code inside
                                              type-
                                process
                            Ø   Multiple languages
                        l   Data access in process
                            Ø   Based on ADO.NET
                            Ø   Same data access programming model
                                as middle -tier
                                   middle-
                        l   SQLTypes
                            Ø   SQL type semantics in managed code
PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                       3
Hosting .NET Runtime
                        l   Safety
                            Ø   Prevent user code from corrupting the server
                                process
                                §   Verifiable code
                            Ø   Use runtime code permissions to control user code
                                when calling
                                §   Unmanaged APIs, user interface, threads, synchronization
                        l   Security
                            Ø   Authorized access to SQL Server data from user
                                code via SQL Server authorization model
                            Ø   Authorized access to system resources from user
                                code via runtime code permissions
                            Ø   Administrators control permissions given
                                to assemblies




                        Three Code Permission Sets
                        l   SAFE
                            Ø   Execute & data access permission
                            Ø   No access to resources outside SQL Server
                            Ø   No unmanaged calls
                            Ø   Must be verifiable
                        l   EXTERNAL_ACCESS
                            Ø   SAFESQL + access to external resources (Net, File
                                permissions)
                            Ø   Requires EXTERNAL ACCESS permission to create
                            Ø   SQL Server will impersonate the caller
                            Ø   Must be verifiable
                        l   UNRESTRICTED
                            Ø   No controls: Can call unmanged code, can be un-verifiable
                                                                            un-
                            Ø   Only Administrators can create


PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                             4
Hosting .NET Runtime
                        l                   Scalability
                                            Ø        As many concurrent users as, as fast as
                                                     Transact-
                                                     Transact-SQL
                                                     §        Integrated SQL Server and runtime threads
                                                     §        Collaboration between SQL Server and GC
                                                     §        SQL Server becomes OS to the .NET runtime

                        l                   Speed
                                            Ø        Efficient data access in process
                                            Ø        Compiled user code, not interpreted as
                                                     Transact-
                                                     Transact-SQL
                                            Ø        Fast transitions in/out of runtime




                        Speed: Functions
                                                             .NET function vs. Transact-SQL inline

                                           800
                         Query time (ms)




                                           600

                                           400
                                                                                                            .NET function vs. Transact-SQL function

                                           200                                                    1000000
                                                                                Query time (ms)




                                                                                                  100000
                                             0
                                                 1       3     5   7   9             10000
                                                                             11 1 3 15 17 19 21                         23   25 27    29
                                                                                      1000
                                                                           Integer ops per func call
                                                                                       100
                                                                            .NET func                  10 TSQL inline
                                                                                                        1
                                                                                                                    100                     1000            10000
                                                                                                                                     Integer ops per call
                         .NET functions approximating speed of Transact-
                                                                  Transact-
                         SQL inline expressions        .NET func TSQL func



                         .NET framework functions much faster than
                         Transact-
                         Transact-SQL functions for complex expressions


PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                                                                                                  5
Outline
                        l   Motivation
                        l   Basic Infrastructure
                            Ø   Hosting overview
                            Ø   Data access inside process: ADO.NET
                            Ø   SQL types
                        l   SQL features
                            Ø   Server assemblies
                            Ø   Functions, procedures, triggers
                            Ø   Types and aggregates
                        l   Summary




                        ADO .NET Data Access
                                                                  Controls,
                            XSL/T, X -Path,
                                   X-                            Designers,
                            Validation, etc                         etc


                                XmlData-
                                XmlData-                          DataSet
                                Document             Sync




                                                            SQL Server
                                XmlReader                                   XmlReader
                                                             Managed
                                                             Managed
                                                              Managed
                            XmlText- XmlNode-
                            XmlText- XmlNode -               Provider
                                                             Provider    XmlText- XmlNode-
                                                                         XmlText- XmlNode -
                             Reader   Reader
                                                              Provider    Reader   Reader



PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                            6
Data Access Inside SQL
                        using System.Data.SqlServer;
                              System.Data.SqlServer;
                        using System.Data.SqlTypes;
                              System.Data.SqlTypes;

                        public class ShippingCosts {
                        public static SqlMoney FreightByShipper( string ship ) {
                                                FreightByShipper(
                          SqlCommand cmd = SqlContext.GetCommand( );
                                                SqlContext.GetCommand(
                          cmd.CommandText = "select sum(o.freight) as freight " +
                          "from orders o join shippers s on o.shipvia = s.shipperid " +
                          "where s.companyname = @CompanyName " ;
                                                     @CompanyName
                          SqlParameter param = cmd.Parameters.Add("@CompanyName",
                                                  cmd.Parameters.Add("@CompanyName",
                                SqlDbType.NVarChar,
                                SqlDbType.NVarChar, COMPANY_NAME_COL_LENGTH);
                          param.Value = ship;
                                          ship;
                          SqlMoney amount = cmd.ExecuteScalar( );
                                                cmd.ExecuteScalar(
                          return amount;
                          }
                        }




                        SQL Types
                        l    Reduce impedance mismatch between
                             programming language and data
                        l    Consistent expression evaluation in
                             mid-
                             mid- and server-tier programming
                                      server-
                        l    SQL Types library
                             Ø   Managed classes: System.Data.SqlTypes
                             Ø   Provide SQL semantics
                                 §   Nullability, three-
                                     Nullability , three-valued logic
                                 §   Precision and scale in operations




PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                        7
SQL Types Example
                        l       Tax function using SQL types
                            using System;
                            using System.Data.SQLTypes;
                            public class myFinances
                            {
                                    public static SQLDouble tax( SQLDouble sal )
                                    {
                                    if ( sal < 50000.0 ) return sal * 0.15;
                                                                      0.15;
                                    if ( sal >= 50000.0 && sal <= 90000.0 ) return sal * 0.23
                                    else return sal * 0.35;
                                                      0.35;
                                    }
                            }

                            SQLDouble makes function NULL-aware
                                                     NULL-




                        Outline
                        l       Motivation
                        l       Basic Infrastructure
                                Ø   Hosting overview
                                Ø   Data access inside process: ADO.NET
                                Ø   SQL types
                        l       SQL features
                                Ø   Server assemblies
                                Ø   Functions, procedures, triggers
                                Ø   Types and aggregates
                        l       Summary


PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                              8
SQL Features
                        l       Assemblies
                        l       Functions
                        l       Procedures
                        l       Triggers
                        l       Types




                        Creating An Assembly
                            CREATE ASSEMBLY geom FROM ‘m1typesgeometry.dll’
                                                       ‘ m1types
                              WITH PERMISSION_SET = SAFE
                              WITH AUTOREGISTER
                            DROP ASSEMBLY lib_geom
                        l   Assemblies stored in database
                            Ø    Backup, restore with data
                        l   Code permissions assigned per assembly
                            Ø    Safe (default), external access, unrestricted
                        l   Autoregister functions
                            Ø    Using .NET custom attributes
                        l   Assembly benefits
                            Ø    Self-
                                 Self-describing metadata: Types, file dependencies
                            Ø    Unit of code deployment: Permissions, versioning




PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                    9
Altering An Assembly
                        l   Cannot invalidate persistent data or indexes
                        l   Implies
                            Ø   No tables with columns of UDT from this assembly
                            Ø   No indexes on functions of this assembly
                        l   Force option allows ALTER even if persistent
                            dependencies exist
                        l   Packaging considerations
                            Ø   Place routines and types in different assemblies




                        Creating A Function
                            CREATE FUNCTION distance (
                              @x1 int, @y1 int, @x2 int, @y2 int ) RETURNS float
                                  int,     int,      int,
                              EXTERNAL NAME ‘geom:CPoint.Distance’
                                                 ‘geom:CPoint.Distance’
                              DETERMINISTIC
                              RETURNS NULL ON NULL INPUT
                            DROP FUNCTION distance
                        l   Functions called from queries
                            Ø   Can be scalar or table -valued
                            Ø   Static class functions
                            Ø   Deterministic functions
                        l   Using a function in a query
                                SELECT s.name FROM Supplier s
                                WHERE dbo.distance ( s.x, s.y, @x, @y ) < 3
                                      dbo.distance(



PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                 10
Creating A Procedure
                             CREATE PROCEDURE check_inventory
                               EXTERNAL NAME ‘events:CInventory.check_level’
                                             ‘events:CInventory.check_level’
                             DROP PROCEDURE check_inventory
                        l    Procedures not called from queries
                             Ø   Can contain SQL queries, updates or DDL
                             Ø   Can return results directly to client




                        Creating A Trigger
                             CREATE TRIGGER supplier_event ON supplier
                               AFTER INSERT, UPDATE
                               EXTERNAL NAME ‘events:CNotif.Supp_Event’
                                              ‘events:CNotif.Supp_Event’
                             DROP TRIGGER supplier_event
                        l    Similar to procedures, plus
                        l    Access to inserted, deleted tables




PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                             11
Creating A Type
                        l   Create or drop a type
                                 CREATE TYPE Point
                                   EXTERNAL NAME ‘geom:Point’
                                                   ‘geom:Point’
                                 DROP TYPE Point

                        l   Using a type
                                 CREATE TABLE Supplier (
                                   id         INTEGER PRIMARY KEY,
                                   name       VARCHAR(20),
                                   location   Point )

                        l   Using a type method in a query
                                 SELECT s.name FROM Supplier s
                                 WHERE s.location::distance( @point ) < 3




                        Query Optimization
                        l    Automatically gather function statistics
                             Ø    Value histograms, execution cost
                        l    Reorder of predicate evaluation
                             Ø    Based on execution cost and statistics
                        l    Function indexes
                             Ø    Speed up expensive functions
                             Ø    Extends computed column indexes
                                  and index views
                        l    Implied and residual predicates


PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                          12
Summary
                        l       Richer server programming model
                                Ø    Any .NET framework language
                                Ø    Same mid - and server -tier data access:
                                           mid-     server-
                                     ADO. NET
                                Ø    Same IDE, project model, debugging, tools
                        l       SQL Server hosting of .NET runtime
                                Ø    Safety, security, scalability, speed
                        l       SQL Server .NET features
                                Ø    Functions, stored procedures, triggers,
                                     types, aggregates




                        Demos
                        l   Sample code for function, stored procedure, and type
                        l   Assemblies
                            Ø       Safe versus external access
                        l   Functions and stored procedures
                            Ø       Creation and execution
                            Ø       Data access in-process via ADO .NET
                                                in-
                        l   Types
                            Ø       Type contract
                            Ø       Create type
                            Ø       Create table with column of user-defined type
                                                                user-
                            Ø       Create index on column of user-defined type
                                                               user-
                            Ø       Insert and query the table




PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                                  13
© 2001 Microsoft Corporation. All rights reserved.




PDC 2001
October 22-26, 2001
© 2001 Microsoft Corporation. All rights reserved.                           14

More Related Content

Viewers also liked

Getting Started With Your Virtual Dedicated Server
Getting Started With Your Virtual Dedicated ServerGetting Started With Your Virtual Dedicated Server
Getting Started With Your Virtual Dedicated Serverwebhostingguy
 
Microsoft Word - SharedHostingSLA
Microsoft Word - SharedHostingSLAMicrosoft Word - SharedHostingSLA
Microsoft Word - SharedHostingSLAwebhostingguy
 
Load Balanced DNS Server A server which cycles through IP ...
Load Balanced DNS Server A server which cycles through IP ...Load Balanced DNS Server A server which cycles through IP ...
Load Balanced DNS Server A server which cycles through IP ...webhostingguy
 
MIS 204 E-Portfolio Presentation
MIS 204 E-Portfolio PresentationMIS 204 E-Portfolio Presentation
MIS 204 E-Portfolio Presentationwebhostingguy
 

Viewers also liked (6)

Getting Started With Your Virtual Dedicated Server
Getting Started With Your Virtual Dedicated ServerGetting Started With Your Virtual Dedicated Server
Getting Started With Your Virtual Dedicated Server
 
NGS
NGSNGS
NGS
 
Microsoft Word - SharedHostingSLA
Microsoft Word - SharedHostingSLAMicrosoft Word - SharedHostingSLA
Microsoft Word - SharedHostingSLA
 
Load Balanced DNS Server A server which cycles through IP ...
Load Balanced DNS Server A server which cycles through IP ...Load Balanced DNS Server A server which cycles through IP ...
Load Balanced DNS Server A server which cycles through IP ...
 
MIS 204 E-Portfolio Presentation
MIS 204 E-Portfolio PresentationMIS 204 E-Portfolio Presentation
MIS 204 E-Portfolio Presentation
 
Acquia Hosting
Acquia HostingAcquia Hosting
Acquia Hosting
 

Similar to SQL Server .NET Programmability Guide

.Net overviewrajnish
.Net overviewrajnish.Net overviewrajnish
.Net overviewrajnishRajnish Kalla
 
Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...
Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...
Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...Novell
 
Net Framework overview
Net Framework overviewNet Framework overview
Net Framework overviewMohitKumar1985
 
Modified.net overview
Modified.net overviewModified.net overview
Modified.net overviewFaisal Aziz
 
Entity Framework v1 and v2
Entity Framework v1 and v2Entity Framework v1 and v2
Entity Framework v1 and v2Eric Nelson
 
Rajnish singh(presentation on oracle )
Rajnish singh(presentation on  oracle )Rajnish singh(presentation on  oracle )
Rajnish singh(presentation on oracle )Rajput Rajnish
 
Brk2051 sql server on linux and docker
Brk2051 sql server on linux and dockerBrk2051 sql server on linux and docker
Brk2051 sql server on linux and dockerBob Ward
 
Cetpa infotech is the best advanced .net training institute in Delhi NCR
Cetpa infotech is the best advanced .net training institute in Delhi NCRCetpa infotech is the best advanced .net training institute in Delhi NCR
Cetpa infotech is the best advanced .net training institute in Delhi NCRACCHITABAJPAI
 
Experience sql server on l inux and docker
Experience sql server on l inux and dockerExperience sql server on l inux and docker
Experience sql server on l inux and dockerBob Ward
 

Similar to SQL Server .NET Programmability Guide (20)

Sql Server - Apresentação
Sql Server - ApresentaçãoSql Server - Apresentação
Sql Server - Apresentação
 
.Net overviewrajnish
.Net overviewrajnish.Net overviewrajnish
.Net overviewrajnish
 
.Net overview by cetpa
.Net overview by cetpa.Net overview by cetpa
.Net overview by cetpa
 
Net overview
Net overviewNet overview
Net overview
 
Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...
Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...
Simplified, Robust and Speedy Novell Identity Manager Implementation with Des...
 
Ow
OwOw
Ow
 
Day2
Day2Day2
Day2
 
Data In Cloud
Data In CloudData In Cloud
Data In Cloud
 
Net overview
Net overviewNet overview
Net overview
 
Net Framework overview
Net Framework overviewNet Framework overview
Net Framework overview
 
DOT Net overview
DOT Net overviewDOT Net overview
DOT Net overview
 
Net framework
Net frameworkNet framework
Net framework
 
Modified.net overview
Modified.net overviewModified.net overview
Modified.net overview
 
Entity Framework v1 and v2
Entity Framework v1 and v2Entity Framework v1 and v2
Entity Framework v1 and v2
 
Rajnish singh(presentation on oracle )
Rajnish singh(presentation on  oracle )Rajnish singh(presentation on  oracle )
Rajnish singh(presentation on oracle )
 
Brk2051 sql server on linux and docker
Brk2051 sql server on linux and dockerBrk2051 sql server on linux and docker
Brk2051 sql server on linux and docker
 
Cetpa infotech is the best advanced .net training institute in Delhi NCR
Cetpa infotech is the best advanced .net training institute in Delhi NCRCetpa infotech is the best advanced .net training institute in Delhi NCR
Cetpa infotech is the best advanced .net training institute in Delhi NCR
 
Experience sql server on l inux and docker
Experience sql server on l inux and dockerExperience sql server on l inux and docker
Experience sql server on l inux and docker
 
E sampark with c#.net
E sampark with c#.netE sampark with c#.net
E sampark with c#.net
 
.Net overview
.Net overview.Net overview
.Net overview
 

More from webhostingguy

Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Frameworkwebhostingguy
 
MySQL and memcached Guide
MySQL and memcached GuideMySQL and memcached Guide
MySQL and memcached Guidewebhostingguy
 
Novell® iChain® 2.3
Novell® iChain® 2.3Novell® iChain® 2.3
Novell® iChain® 2.3webhostingguy
 
Load-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web serversLoad-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web serverswebhostingguy
 
SQL Server 2008 Consolidation
SQL Server 2008 ConsolidationSQL Server 2008 Consolidation
SQL Server 2008 Consolidationwebhostingguy
 
Master Service Agreement
Master Service AgreementMaster Service Agreement
Master Service Agreementwebhostingguy
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...webhostingguy
 
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...webhostingguy
 
Managing Diverse IT Infrastructure
Managing Diverse IT InfrastructureManaging Diverse IT Infrastructure
Managing Diverse IT Infrastructurewebhostingguy
 
Web design for business.ppt
Web design for business.pptWeb design for business.ppt
Web design for business.pptwebhostingguy
 
IT Power Management Strategy
IT Power Management Strategy IT Power Management Strategy
IT Power Management Strategy webhostingguy
 
Excel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for MerchandisersExcel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for Merchandiserswebhostingguy
 
Parallels Hosting Products
Parallels Hosting ProductsParallels Hosting Products
Parallels Hosting Productswebhostingguy
 
Microsoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 MbMicrosoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 Mbwebhostingguy
 

More from webhostingguy (20)

File Upload
File UploadFile Upload
File Upload
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Framework
 
MySQL and memcached Guide
MySQL and memcached GuideMySQL and memcached Guide
MySQL and memcached Guide
 
Novell® iChain® 2.3
Novell® iChain® 2.3Novell® iChain® 2.3
Novell® iChain® 2.3
 
Load-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web serversLoad-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web servers
 
SQL Server 2008 Consolidation
SQL Server 2008 ConsolidationSQL Server 2008 Consolidation
SQL Server 2008 Consolidation
 
What is mod_perl?
What is mod_perl?What is mod_perl?
What is mod_perl?
 
What is mod_perl?
What is mod_perl?What is mod_perl?
What is mod_perl?
 
Master Service Agreement
Master Service AgreementMaster Service Agreement
Master Service Agreement
 
Notes8
Notes8Notes8
Notes8
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
 
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
 
Managing Diverse IT Infrastructure
Managing Diverse IT InfrastructureManaging Diverse IT Infrastructure
Managing Diverse IT Infrastructure
 
Web design for business.ppt
Web design for business.pptWeb design for business.ppt
Web design for business.ppt
 
IT Power Management Strategy
IT Power Management Strategy IT Power Management Strategy
IT Power Management Strategy
 
Excel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for MerchandisersExcel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for Merchandisers
 
OLUG_xen.ppt
OLUG_xen.pptOLUG_xen.ppt
OLUG_xen.ppt
 
Parallels Hosting Products
Parallels Hosting ProductsParallels Hosting Products
Parallels Hosting Products
 
Microsoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 MbMicrosoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 Mb
 
Reseller's Guide
Reseller's GuideReseller's Guide
Reseller's Guide
 

SQL Server .NET Programmability Guide

  • 1. SQL Server™ .NET Programmability José A. Blakeley Architect SQL Server Background l Transact SQL (T -SQL) Ø SQL Server’s database programming language Ø Includes: § SQL DDL, SQL DML § Variables, assignment, iteration, procedures, functions l .NET Platform Ø .NET runtime (Common Language Runtime) § Verifiable Intermediate Language § Managed memory – garbage collection § Multiple languages (e.g, C#, C++, Cobol) (e.g , Ø Frameworks library (e.g., GUI, Files, XML) Ø Assemblies – New Dynamic Link Libraries § Contain code + metadata (class definitions, assembly dependencies) § Unit of code deployment, security, versioning PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 1
  • 2. Outline l Motivation l Basic Infrastructure Ø Hosting overview Ø Data access inside process: ADO.NET Ø SQL types l SQL features Ø Server assemblies Ø Functions, procedures, triggers Ø Types and aggregates l Summary Motivation l Broaden the set of languages for running data- data -intensive business logic in server Ø IT Developers and ISVs Ø Multiple languages: Visual Basic ®, C#, C++, J#, … Ø Same mid - and server -tier data access model mid- server- Ø Same tools: IDE, project model, debugging, … Ø Transact- Transact-SQL still supported l Packaged as .NET assemblies Ø Verifiable, secure l Deployed to SQL Server as Ø Functions, procedures, triggers, types l Basis for SQL Server Extensibility PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 2
  • 3. Development Steps VB, C#, C++ Build VS Assembly: Project “TaxLib.dll” TaxLib.dll” Runtime hosted inside SQL Data Definition: create assembly … SQL Server create function … create procedure … create trigger … create type … SQL Queries: SQL Server select sum(tax(sal,state) sum(tax(sal,state) ) from Emp where county = ‘King’ Basic Infrastructure l Hosting common language runtime inside SQL Server Ø 4Ss: Safety, security, scalability, speed Ø Run verified, type-safe code inside type- process Ø Multiple languages l Data access in process Ø Based on ADO.NET Ø Same data access programming model as middle -tier middle- l SQLTypes Ø SQL type semantics in managed code PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 3
  • 4. Hosting .NET Runtime l Safety Ø Prevent user code from corrupting the server process § Verifiable code Ø Use runtime code permissions to control user code when calling § Unmanaged APIs, user interface, threads, synchronization l Security Ø Authorized access to SQL Server data from user code via SQL Server authorization model Ø Authorized access to system resources from user code via runtime code permissions Ø Administrators control permissions given to assemblies Three Code Permission Sets l SAFE Ø Execute & data access permission Ø No access to resources outside SQL Server Ø No unmanaged calls Ø Must be verifiable l EXTERNAL_ACCESS Ø SAFESQL + access to external resources (Net, File permissions) Ø Requires EXTERNAL ACCESS permission to create Ø SQL Server will impersonate the caller Ø Must be verifiable l UNRESTRICTED Ø No controls: Can call unmanged code, can be un-verifiable un- Ø Only Administrators can create PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 4
  • 5. Hosting .NET Runtime l Scalability Ø As many concurrent users as, as fast as Transact- Transact-SQL § Integrated SQL Server and runtime threads § Collaboration between SQL Server and GC § SQL Server becomes OS to the .NET runtime l Speed Ø Efficient data access in process Ø Compiled user code, not interpreted as Transact- Transact-SQL Ø Fast transitions in/out of runtime Speed: Functions .NET function vs. Transact-SQL inline 800 Query time (ms) 600 400 .NET function vs. Transact-SQL function 200 1000000 Query time (ms) 100000 0 1 3 5 7 9 10000 11 1 3 15 17 19 21 23 25 27 29 1000 Integer ops per func call 100 .NET func 10 TSQL inline 1 100 1000 10000 Integer ops per call .NET functions approximating speed of Transact- Transact- SQL inline expressions .NET func TSQL func .NET framework functions much faster than Transact- Transact-SQL functions for complex expressions PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 5
  • 6. Outline l Motivation l Basic Infrastructure Ø Hosting overview Ø Data access inside process: ADO.NET Ø SQL types l SQL features Ø Server assemblies Ø Functions, procedures, triggers Ø Types and aggregates l Summary ADO .NET Data Access Controls, XSL/T, X -Path, X- Designers, Validation, etc etc XmlData- XmlData- DataSet Document Sync SQL Server XmlReader XmlReader Managed Managed Managed XmlText- XmlNode- XmlText- XmlNode - Provider Provider XmlText- XmlNode- XmlText- XmlNode - Reader Reader Provider Reader Reader PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 6
  • 7. Data Access Inside SQL using System.Data.SqlServer; System.Data.SqlServer; using System.Data.SqlTypes; System.Data.SqlTypes; public class ShippingCosts { public static SqlMoney FreightByShipper( string ship ) { FreightByShipper( SqlCommand cmd = SqlContext.GetCommand( ); SqlContext.GetCommand( cmd.CommandText = "select sum(o.freight) as freight " + "from orders o join shippers s on o.shipvia = s.shipperid " + "where s.companyname = @CompanyName " ; @CompanyName SqlParameter param = cmd.Parameters.Add("@CompanyName", cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, SqlDbType.NVarChar, COMPANY_NAME_COL_LENGTH); param.Value = ship; ship; SqlMoney amount = cmd.ExecuteScalar( ); cmd.ExecuteScalar( return amount; } } SQL Types l Reduce impedance mismatch between programming language and data l Consistent expression evaluation in mid- mid- and server-tier programming server- l SQL Types library Ø Managed classes: System.Data.SqlTypes Ø Provide SQL semantics § Nullability, three- Nullability , three-valued logic § Precision and scale in operations PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 7
  • 8. SQL Types Example l Tax function using SQL types using System; using System.Data.SQLTypes; public class myFinances { public static SQLDouble tax( SQLDouble sal ) { if ( sal < 50000.0 ) return sal * 0.15; 0.15; if ( sal >= 50000.0 && sal <= 90000.0 ) return sal * 0.23 else return sal * 0.35; 0.35; } } SQLDouble makes function NULL-aware NULL- Outline l Motivation l Basic Infrastructure Ø Hosting overview Ø Data access inside process: ADO.NET Ø SQL types l SQL features Ø Server assemblies Ø Functions, procedures, triggers Ø Types and aggregates l Summary PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 8
  • 9. SQL Features l Assemblies l Functions l Procedures l Triggers l Types Creating An Assembly CREATE ASSEMBLY geom FROM ‘m1typesgeometry.dll’ ‘ m1types WITH PERMISSION_SET = SAFE WITH AUTOREGISTER DROP ASSEMBLY lib_geom l Assemblies stored in database Ø Backup, restore with data l Code permissions assigned per assembly Ø Safe (default), external access, unrestricted l Autoregister functions Ø Using .NET custom attributes l Assembly benefits Ø Self- Self-describing metadata: Types, file dependencies Ø Unit of code deployment: Permissions, versioning PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 9
  • 10. Altering An Assembly l Cannot invalidate persistent data or indexes l Implies Ø No tables with columns of UDT from this assembly Ø No indexes on functions of this assembly l Force option allows ALTER even if persistent dependencies exist l Packaging considerations Ø Place routines and types in different assemblies Creating A Function CREATE FUNCTION distance ( @x1 int, @y1 int, @x2 int, @y2 int ) RETURNS float int, int, int, EXTERNAL NAME ‘geom:CPoint.Distance’ ‘geom:CPoint.Distance’ DETERMINISTIC RETURNS NULL ON NULL INPUT DROP FUNCTION distance l Functions called from queries Ø Can be scalar or table -valued Ø Static class functions Ø Deterministic functions l Using a function in a query SELECT s.name FROM Supplier s WHERE dbo.distance ( s.x, s.y, @x, @y ) < 3 dbo.distance( PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 10
  • 11. Creating A Procedure CREATE PROCEDURE check_inventory EXTERNAL NAME ‘events:CInventory.check_level’ ‘events:CInventory.check_level’ DROP PROCEDURE check_inventory l Procedures not called from queries Ø Can contain SQL queries, updates or DDL Ø Can return results directly to client Creating A Trigger CREATE TRIGGER supplier_event ON supplier AFTER INSERT, UPDATE EXTERNAL NAME ‘events:CNotif.Supp_Event’ ‘events:CNotif.Supp_Event’ DROP TRIGGER supplier_event l Similar to procedures, plus l Access to inserted, deleted tables PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 11
  • 12. Creating A Type l Create or drop a type CREATE TYPE Point EXTERNAL NAME ‘geom:Point’ ‘geom:Point’ DROP TYPE Point l Using a type CREATE TABLE Supplier ( id INTEGER PRIMARY KEY, name VARCHAR(20), location Point ) l Using a type method in a query SELECT s.name FROM Supplier s WHERE s.location::distance( @point ) < 3 Query Optimization l Automatically gather function statistics Ø Value histograms, execution cost l Reorder of predicate evaluation Ø Based on execution cost and statistics l Function indexes Ø Speed up expensive functions Ø Extends computed column indexes and index views l Implied and residual predicates PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 12
  • 13. Summary l Richer server programming model Ø Any .NET framework language Ø Same mid - and server -tier data access: mid- server- ADO. NET Ø Same IDE, project model, debugging, tools l SQL Server hosting of .NET runtime Ø Safety, security, scalability, speed l SQL Server .NET features Ø Functions, stored procedures, triggers, types, aggregates Demos l Sample code for function, stored procedure, and type l Assemblies Ø Safe versus external access l Functions and stored procedures Ø Creation and execution Ø Data access in-process via ADO .NET in- l Types Ø Type contract Ø Create type Ø Create table with column of user-defined type user- Ø Create index on column of user-defined type user- Ø Insert and query the table PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 13
  • 14. © 2001 Microsoft Corporation. All rights reserved. PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 14