Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sofia, Bulgaria | 9-10 October TSQL Enhancements in SQL Server 2005 Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte.

Similar presentations


Presentation on theme: "Sofia, Bulgaria | 9-10 October TSQL Enhancements in SQL Server 2005 Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte."— Presentation transcript:

1 Sofia, Bulgaria | 9-10 October TSQL Enhancements in SQL Server 2005 Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA)

2 Sofia, Bulgaria | 9-10 October Speaker.Bio.ToString() ●CTO and co-Founder of Corzen, Inc ●Microsoft RD, MVP and INETA Speaker ●International Conference Speaker for 9+ Years ●Wrote a few books on databases and development ● SQL Server 2005 Developers Guide (MS Press) ●Co-moderator & founder of NYC.NET Developers Group ●http://www.nycdotnetdev.com ●Former CTO of Zagat Survey ●CTO and co-Founder of Corzen, Inc ●Microsoft RD, MVP and INETA Speaker ●International Conference Speaker for 9+ Years ●Wrote a few books on databases and development ● SQL Server 2005 Developers Guide (MS Press) ●Co-moderator & founder of NYC.NET Developers Group ●http://www.nycdotnetdev.com ●Former CTO of Zagat Survey

3 Sofia, Bulgaria | 9-10 October Agenda ●Introduction ●New data types ●XML, varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling ●Introduction ●New data types ●XML, varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling

4 Sofia, Bulgaria | 9-10 October Introduction ●TSQL is here to stay, even though you can code SPs in CLR Languages ●Richer data types ●XML, date/time types, varchar(max) ●More expressive power ●Recursive queries, TOP ( ), new relational operators, etc. ●Very cool and long awaited features ●Transaction abort handlers, LOB enhancements through varchar(max) ●TSQL is here to stay, even though you can code SPs in CLR Languages ●Richer data types ●XML, date/time types, varchar(max) ●More expressive power ●Recursive queries, TOP ( ), new relational operators, etc. ●Very cool and long awaited features ●Transaction abort handlers, LOB enhancements through varchar(max)

5 Sofia, Bulgaria | 9-10 October Introduction SQL versus CLR ●Full interoperability with CLR features ●T-SQL → CLR → T-SQL ●When to use CLR versus T-SQL ●Computation intensive: computational logic, arithmetic operations: CLR ●Data access intensive: T-SQL ●Full interoperability with CLR features ●T-SQL → CLR → T-SQL ●When to use CLR versus T-SQL ●Computation intensive: computational logic, arithmetic operations: CLR ●Data access intensive: T-SQL

6 Sofia, Bulgaria | 9-10 October Agenda ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Queuing enhancements ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Queuing enhancements

7 Sofia, Bulgaria | 9-10 October New Date/Time Types Frequently asked for… ●XML ●(n)varchar(Max) ●XML ●(n)varchar(Max)

8 Sofia, Bulgaria | 9-10 October Varchar(max) Type ●Extension to varchar, nvarchar, varbinary up to 2GB ●Uses MAX size specifier CREATE TABLE myTable (Id int, Picture varbinary(max)) ●Alternative to text/ntext/image ●No text pointer support ●Extension to varchar, nvarchar, varbinary up to 2GB ●Uses MAX size specifier CREATE TABLE myTable (Id int, Picture varbinary(max)) ●Alternative to text/ntext/image ●No text pointer support

9 Sofia, Bulgaria | 9-10 October Varchar(max) Type ●SELECT + DML ●Conversions ●Concatenation ●Comparisons ●Triggers ●SELECT + DML ●Conversions ●Concatenation ●Comparisons ●Triggers ●Aggregates ●Functions ●Parameters ●Variables Brings together programming model of small and large values Brings together programming model of small and large values

10 Sofia, Bulgaria | 9-10 October Varchar(max) Type ●All string functions operate on varchar(max) ●SUBSTRING used to read chunks ●UPDATE statement enhanced to support update of CHUNKS UPDATE myTable SET Picture::Write(@newchunk, @offset, @remove) ●All string functions operate on varchar(max) ●SUBSTRING used to read chunks ●UPDATE statement enhanced to support update of CHUNKS UPDATE myTable SET Picture::Write(@newchunk, @offset, @remove)

11 Sofia, Bulgaria | 9-10 October XML Datatype varchar(max)

12 Sofia, Bulgaria | 9-10 October Agenda ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling

13 Sofia, Bulgaria | 9-10 October Top Enhancements Enhancement from SQL Server 2000 ●In SS 2000: SELECT TOP ●In Yukon: SELECT TOP ( ) ●Also supported in INSERT, UPDATE, DELETE ● may be for example a variable, subquery (self-contained returning numeric scalar) ●In SS 2000: SELECT TOP ●In Yukon: SELECT TOP ( ) ●Also supported in INSERT, UPDATE, DELETE ● may be for example a variable, subquery (self-contained returning numeric scalar)

14 Sofia, Bulgaria | 9-10 October Top Enhancements Partial Deprecation of SET ROWCOUNT in SQL 2005 ●SET ROWCOUNT still works for SELECTS ●SET ROWCOUNT is ignored in UPDATE, DELETE, INSERT unless the server is running in backward compatibility mode ●In SQL 2000 you had to add TOP to every single UPDATE, DELETE and INSERT query plan because we don’t know if it will be activated at query run time ●SET ROWCOUNT still works for SELECTS ●SET ROWCOUNT is ignored in UPDATE, DELETE, INSERT unless the server is running in backward compatibility mode ●In SQL 2000 you had to add TOP to every single UPDATE, DELETE and INSERT query plan because we don’t know if it will be activated at query run time

15 Sofia, Bulgaria | 9-10 October Top Enhancements

16 Sofia, Bulgaria | 9-10 October Agenda ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling

17 Sofia, Bulgaria | 9-10 October Recursive Queries And CTE Common table expression ●Very similar to a view, but imbedded in the query WITH CTE_name (colname1, colname2,..) as (SELECT …..) SELECT …. ●May be used in front of SELECT, UPDATE, DELETE, INSERT ●Several CTEs may be in a single statement ●There must be semicolon in front of the WITH if this is not the first statement in batch (takes care of ambiguities) ●Very similar to a view, but imbedded in the query WITH CTE_name (colname1, colname2,..) as (SELECT …..) SELECT …. ●May be used in front of SELECT, UPDATE, DELETE, INSERT ●Several CTEs may be in a single statement ●There must be semicolon in front of the WITH if this is not the first statement in batch (takes care of ambiguities)

18 Sofia, Bulgaria | 9-10 October Recursive Queries And CTE Recursive query ●This is the main reason we have introduced the CTEs ●Recursion is achieved by allowing the CTE refer to itself ●Such CTE must have special syntax WITH REC_CTE as (SELECT1 UNION ALL SELECT2) ●This is the main reason we have introduced the CTEs ●Recursion is achieved by allowing the CTE refer to itself ●Such CTE must have special syntax WITH REC_CTE as (SELECT1 UNION ALL SELECT2)

19 Sofia, Bulgaria | 9-10 October Recursive Queries And CTE Recursive query ●SELECT1 does not refer to REC_CTE and it is called Anchor Member ●SELECT2 references REC_CTE and is called Recursive Member ●Semantics ●Execute the Anchor member, get the set T 0 ●Execute the Recursive member with T i as an input T i+1 as an output ●Repeat previous step until you get empty set (say T n+1 ) ●The result is UNION ALL of T 0 to T n ●SELECT1 does not refer to REC_CTE and it is called Anchor Member ●SELECT2 references REC_CTE and is called Recursive Member ●Semantics ●Execute the Anchor member, get the set T 0 ●Execute the Recursive member with T i as an input T i+1 as an output ●Repeat previous step until you get empty set (say T n+1 ) ●The result is UNION ALL of T 0 to T n

20 Sofia, Bulgaria | 9-10 October Recursive Queries And CTE Tidbits ●We are using ANSI syntax and semantics ●Watch for “runaway recursion” – there is a server-wide limit with default 100 levels ●You can overrule the limit on your query by OPTION (MAXRECURSION n) ●N=0 means no limit! ●You can programmatically set your own limit by a predicate on the Recursive Member ●We are using ANSI syntax and semantics ●Watch for “runaway recursion” – there is a server-wide limit with default 100 levels ●You can overrule the limit on your query by OPTION (MAXRECURSION n) ●N=0 means no limit! ●You can programmatically set your own limit by a predicate on the Recursive Member

21 Sofia, Bulgaria | 9-10 October Common Table Expressions

22 Sofia, Bulgaria | 9-10 October Agenda ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling

23 Sofia, Bulgaria | 9-10 October New Relational Operators PIVOT and UNPIVOT ●PIVOT ●Transforms a set of rows to columns ●Similar to Access TRANSFORM ●Useful for open schemas/OLAP scenarios ●UNPIVOT ●Reverse operation of PIVOT ●PIVOT ●Transforms a set of rows to columns ●Similar to Access TRANSFORM ●Useful for open schemas/OLAP scenarios ●UNPIVOT ●Reverse operation of PIVOT

24 Sofia, Bulgaria | 9-10 October New Relational Operators PIVOT SELECT * FROM table PIVOT(MIN(PropVal) FOR PropName IN ([Name], [Author])) SELECT * FROM table PIVOT(MIN(PropVal) FOR PropName IN ([Name], [Author])) ObjI D PropNamePropVal 1Namex.doc 1Namespec.doc 1CrDate12/3/2001 2Namesales.xls 2AuthorMary Higgins ObjIDNameAuthor 1Spec.docNULL 2Sales.xlsMary Higgins

25 Sofia, Bulgaria | 9-10 October New Relational Operators APPLY ●Evaluates a table-valued function for every row of its left input ●Rows: UNION of all evaluations ●Columns: Columns of left input + columns of TVF ●Useful in conjunction with complex data types (e.g., spatial) ●Evaluates a table-valued function for every row of its left input ●Rows: UNION of all evaluations ●Columns: Columns of left input + columns of TVF ●Useful in conjunction with complex data types (e.g., spatial)

26 Sofia, Bulgaria | 9-10 October Pivot (static and dynamic)

27 Sofia, Bulgaria | 9-10 October Agenda ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling ●Introduction ●New data types ●XML, Varchar(max) ●More expressive power ●Top enhancements ●Recursive query and CTE ●PIVOT/APPLY ●Transact abort handling

28 Sofia, Bulgaria | 9-10 October Tran Abort Handling ●Catch Transaction Abort Errors ●TRY / CATCH model ●No loss of transaction context ●“Doomed” Transaction ●Catch Transaction Abort Errors ●TRY / CATCH model ●No loss of transaction context ●“Doomed” Transaction

29 Sofia, Bulgaria | 9-10 October Tran Abort Handling Syntax BEGIN TRY sql statement [,…n] END TRY BEGIN CATCH TRAN_ABORT sql statement [,…n] END CATCH BEGIN TRY sql statement [,…n] END TRY BEGIN CATCH TRAN_ABORT sql statement [,…n] END CATCH

30 Sofia, Bulgaria | 9-10 October Tran Abort Handling Details ●@@error can be examined as first statement in CATCH block ●No DML or DDL succeeds in a Doomed Transaction ●ROLLBACK allows creation of new transaction context ●@@error can be examined as first statement in CATCH block ●No DML or DDL succeeds in a Doomed Transaction ●ROLLBACK allows creation of new transaction context

31 Sofia, Bulgaria | 9-10 October RAISERROR WITH TRAN_ABORT ●New option for RAISERROR ●Aborts transaction and batch ●If executed within TRY block, control moves to CATCH block ●@@error is set and can be examined in CATCH block ●New option for RAISERROR ●Aborts transaction and batch ●If executed within TRY block, control moves to CATCH block ●@@error is set and can be examined in CATCH block

32 Sofia, Bulgaria | 9-10 October Questions?

33 Sofia, Bulgaria | 9-10 October Thanks! ●Please fill out your evaluation form! ●stevef@orcsweb.comstevef@orcsweb.com ●Please put (TSQL in the subject) ●Please fill out your evaluation form! ●stevef@orcsweb.comstevef@orcsweb.com ●Please put (TSQL in the subject)


Download ppt "Sofia, Bulgaria | 9-10 October TSQL Enhancements in SQL Server 2005 Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen Forte."

Similar presentations


Ads by Google