SQL Server SQL Server

780 views

Published on

  • Be the first to comment

SQL Server SQL Server

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 14. © 2001 Microsoft Corporation. All rights reserved. PDC 2001 October 22-26, 2001 © 2001 Microsoft Corporation. All rights reserved. 14

×