sofia, bulgaria | 9-10 october tsql enhancements in sql server 2005 stephen forte cto, corzen inc...

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

Upload: percival-palmer

Post on 27-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

Sofia, Bulgaria | 9-10 October

TSQL Enhancements in SQL Server 2005TSQL 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)

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

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

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

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

Sofia, Bulgaria | 9-10 October

IntroductionIntroduction

● 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 (<expression>), 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 (<expression>), new relational operators, etc.

● Very cool and long awaited features● Transaction abort handlers, LOB

enhancements through varchar(max)

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

Sofia, Bulgaria | 9-10 October

IntroductionSQL versus CLRIntroductionSQL 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

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

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

Sofia, Bulgaria | 9-10 October

New Date/Time TypesFrequently asked for…New Date/Time TypesFrequently asked for…

● XML

● (n)varchar(Max)

● XML

● (n)varchar(Max)

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

Sofia, Bulgaria | 9-10 October

Varchar(max) TypeVarchar(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

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

Sofia, Bulgaria | 9-10 October

Varchar(max) TypeVarchar(max) Type

● SELECT + DML

● Conversions

● Concatenation

● Comparisons

● Triggers

● SELECT + DML

● Conversions

● Concatenation

● Comparisons

● Triggers

● Aggregates

● Functions

● Parameters

● Variables

● Aggregates

● Functions

● Parameters

● Variables

Brings together programming model Brings together programming model of small and large valuesof small and large values

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

Sofia, Bulgaria | 9-10 October

Varchar(max) TypeVarchar(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)

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

Sofia, Bulgaria | 9-10 October

XML DatatypeXML Datatypevarchar(max)varchar(max)

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

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

Sofia, Bulgaria | 9-10 October

Top EnhancementsEnhancement from SQL Server 2000Top EnhancementsEnhancement from SQL Server 2000

● In SS 2000: SELECT TOP <const.>

● In Yukon: SELECT TOP (<expression>)

● Also supported in INSERT, UPDATE, DELETE

● <expression> may be for example a variable, subquery (self-contained returning numeric scalar)

● In SS 2000: SELECT TOP <const.>

● In Yukon: SELECT TOP (<expression>)

● Also supported in INSERT, UPDATE, DELETE

● <expression> may be for example a variable, subquery (self-contained returning numeric scalar)

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

Sofia, Bulgaria | 9-10 October

Top EnhancementsPartial Deprecation of SET ROWCOUNT in SQL 2005

Top EnhancementsPartial 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

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

Sofia, Bulgaria | 9-10 October

Top EnhancementsTop Enhancements

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

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

Sofia, Bulgaria | 9-10 October

Recursive Queries And CTECommon table expressionRecursive Queries And CTECommon table expression

● Very similar to a view, but imbedded in the query

WITH CTE_name (colname1, colname2,..) as (SELECT …..) SELECT ….<the main query that references CTE_name>● 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 ….<the main query that references CTE_name>● 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)

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

Sofia, Bulgaria | 9-10 October

Recursive Queries And CTERecursive queryRecursive Queries And CTERecursive 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)

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

Sofia, Bulgaria | 9-10 October

Recursive Queries And CTERecursive queryRecursive Queries And CTERecursive 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 T0

● Execute the Recursive member with Ti as an input Ti+1 as an output

● Repeat previous step until you get empty set (say Tn+1)

● The result is UNION ALL of T0 to Tn

● 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 T0

● Execute the Recursive member with Ti as an input Ti+1 as an output

● Repeat previous step until you get empty set (say Tn+1)

● The result is UNION ALL of T0 to Tn

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

Sofia, Bulgaria | 9-10 October

Recursive Queries And CTETidbitsRecursive Queries And CTETidbits

● 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

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

Sofia, Bulgaria | 9-10 October

Common Table Common Table ExpressionsExpressions

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

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

Sofia, Bulgaria | 9-10 October

New Relational Operators PIVOT and UNPIVOTNew 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

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

Sofia, Bulgaria | 9-10 October

New Relational OperatorsPIVOTNew Relational OperatorsPIVOT

SELECT *

FROM table PIVOT(MIN(PropVal) FOR PropName IN ([Name], [Author]))

SELECT *

FROM table PIVOT(MIN(PropVal) FOR PropName IN ([Name], [Author]))

ObjID

PropName PropVal

1 Name x.doc

1 Name spec.doc

1 CrDate 12/3/2001

2 Name sales.xls

2 Author Mary Higgins

ObjID Name Author

1 Spec.doc NULL

2 Sales.xls Mary Higgins

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

Sofia, Bulgaria | 9-10 October

New Relational OperatorsAPPLYNew Relational OperatorsAPPLY

● 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)

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

Sofia, Bulgaria | 9-10 October

Pivot (static and dynamic)Pivot (static and dynamic)

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

Sofia, Bulgaria | 9-10 October

AgendaAgenda

● 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

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

Sofia, Bulgaria | 9-10 October

Tran Abort HandlingTran 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

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

Sofia, Bulgaria | 9-10 October

Tran Abort HandlingSyntaxTran Abort HandlingSyntax

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

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

Sofia, Bulgaria | 9-10 October

Tran Abort HandlingDetailsTran Abort HandlingDetails

● @@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

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

Sofia, Bulgaria | 9-10 October

RAISERROR WITH TRAN_ABORTRAISERROR 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

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

Sofia, Bulgaria | 9-10 October

Questions?Questions?

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

Sofia, Bulgaria | 9-10 October

Thanks!Thanks!

● Please fill out your evaluation form!

[email protected]

● Please put (TSQL in the subject)

● Please fill out your evaluation form!

[email protected]

● Please put (TSQL in the subject)