Download presentation
Presentation is loading. Please wait.
1
T-SQL and XML Enhancements in SQL Server 2005
1/3/2019 9:29 AM T-SQL and XML Enhancements in SQL Server 2005 Eric Nelson Application Architect Microsoft (SQL Dev and UK ISV) NOTE: UPDATED SLIDES – this version will be made available for download in a few days. © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
2
Many TSQL enhancements…
Exception handling TOP(expression) PIVOT/UNPIVOT APPLY Common Table Expressions Hierarchical queries RANK SNAPSHOT Isolation VARCHAR(MAX), VARBINARY(MAX)… XML Type Full Text Search improvements WAITFOR Event Notifcations on DDL and DML FOR XML TABLESAMPLE Service Broker BULK INSERT … Plus “none dev TSQL stuff” Online Index Rebuild Schemas Certificates Statement Level Recompile …
3
“If @@Error” programming sucks! There must be a better way…
Exception Handling “If programming sucks! There must be a better way…
4
Exception Handling: TRY/CATCH
1/3/2019 9:29 AM Exception Handling: TRY/CATCH Eliminate tedious “if code Perform logging/cleanup when exceptions occur Ability to re-raise exceptions after cleanup BEGIN TRY <core logic> END TRY BEGIN CATCH TRAN_ABORT <exception handling logic> END CATCH © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
5
Exception handling In CATCH block you can Transaction abort
1/3/2019 9:29 AM Exception handling In CATCH block you can Use new built-in functions to retrieve error-number, message, severity Re-raise original exception or raise an alt Transaction abort Tx remains in “doomed” state until explicitly rolled back No actions which result in log writes may be performed in a doomed transaction – SELECT only until you ROLLBACK © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
6
Transaction Abort Handling: Example
CREATE PROCEDURE nvarchar(1000) AS int, @error_severity int BEGIN TRY BEGIN TRAN --Constraint violations cause txn/batch-abort; INSERT T1 COMMIT TRAN END TRY BEGIN CATCH TRAN_ABORT ROLLBACK INSERT T1_Log GETDATE()) SELECT @error_msg=error_message(), @error_num=error_number(), @error_severity=error_severity() END CATCH © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
7
Drat …. I will not know until runtime how many rows I need…
TOP Drat …. I will not know until runtime how many rows I need…
8
TOP (<expression>)
1/3/2019 9:29 AM TOP (<expression>) SQL 7.0 and 2000 Provided TOP (n) with constant expression Only for SELECT SQL Server 2005 Provides TOP (<expression>) Also available on INSERT/UPDATE/DELETE int -- at execution time SELECT * FROM T © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
9
1/3/2019 9:29 AM TRY/CATCH TOP © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
10
PIVOT Columns, columns everywhere …. I need rows!
(And Access can already do it!)
11
PIVOT PIVOT UNPIVOT Transforms a set of rows to columns
1/3/2019 9:29 AM PIVOT PIVOT Transforms a set of rows to columns Similar to Access TRANSFORM Useful for data analysis Useful for open shemas E.g. Products with different properties UNPIVOT Reverse operation of PIVOT © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
12
Example: Simple PIVOT Make 1990 1991 Honda 2000 3000 Acura 500 600
Year Sales Honda 1990 2000 Acura 500 1991 3000 600 SELECT * FROM SalesTable PIVOT(SUM(Sales) FOR Year IN ([1990], [1991])) s Make 1990 1991 Honda 2000 3000 Acura 500 600 © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
13
1/3/2019 9:29 AM PIVOT © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
14
Common Table Expressions
Temporary tables make some things so much easier to code– but that is so clunky… I need something better…
15
Common Table Expressions
1/3/2019 9:29 AM Common Table Expressions As per SQL-99 Syntax: WITH <CTEName> ( <column-list> ) AS ( <CTE>) <SELECT using CTE> Both recursive and non-recursive forms Non-recursive: Tidy code, avoid temp tables, views, sub selects Recursive: Rewrite queries with derived tables to be more readable © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
16
Example: CTE --Average number of times a Product was ordered
--for all Products that appeared on an order --more than 50 times WITH SalesCTE(ProductID, SalesOrderID) AS ( SELECT ProductID, COUNT(SalesOrderID) FROM Sales.SalesOrderDetail GROUP BY ProductID ) SELECT AVG(SalesOrderID) FROM SalesCTE WHERE SalesOrderID > 50
17
1/3/2019 9:29 AM Simple CTE © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
18
Recursive CTEs Recursive, when <CTE> references itself
1/3/2019 9:29 AM Recursive CTEs Recursive, when <CTE> references itself Recursive form of CTE <non-recursive SELECT> UNION ALL <SELECT referencing CTE> Recursion stops when 2nd SELECT produces empty results Initialize Accumulate © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
19
EXAMPLE: “Org Chart” No Recursive Queries
int -- table variable to hold accumulated results TABLE (empid nchar(5) primary key, empname nvarchar(50) NOT NULL, mgrid nchar(5), title nvarchar(30), processed tinyint default 0) -- with direct reports of the given employee SELECT empid, empname, mgrid, title, 0 FROM employees WHERE empid = ‘12345’ = -- While new employees were added in the previous iteration > 0 BEGIN /*Mark all employee records whose direct reports are going to be found in this iteration with processed=1.*/ SET processed = 1 WHERE processed = 0 -- Insert employees who report to employees marked 1. SELECT e.empid, e.empname, e.mgrid, e.title, 0 FROM employees r WHERE e.mgrid=r.empid and e.mgrid <> e.empid and r.processed = 1 /*Mark all employee records whose direct reports have been found in this iteration.*/ SET processed = 2 WHERE processed = 1 END © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
20
EXAMPLE: “Org Chart” With Recursive Queries
WITH EmpCTE(empid, empname, mgrid) AS ( SELECT empid, empname, mgrid FROM Employees WHERE empid = ‘12345’ UNION ALL SELECT E.empid, E.empname, E.mgrid FROM Employees AS E JOIN EmpCTE AS M ON E.mgrid = M.empid ) SELECT * FROM EmpCTE © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
21
I need to control how people change the schema of my database…how?
DDL Triggers I need to control how people change the schema of my database…how?
22
DDL Triggers Extension of traditional triggers for DDL events
1/3/2019 9:29 AM DDL Triggers Extension of traditional triggers for DDL events Triggering events include all DDL statements CREATE_TABLE, ALTER_PROCEDURE, DROP_LOGIN, etc. Scoping at Database and Server levels Event data available inside trigger through eventdata() function © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
23
EXAMPLE: DDL Triggers -- Log tables being dropped to dropLog
CREATE TABLE dropLog (id INT PRIMARY KEY IDENTITY, logTxt VARCHAR(MAX)) GO -- Trigger to log drops CREATE TRIGGER ddlDrop ON DATABASE AFTER DROP_TABLE AS INSERT INTO dropLog VALUES('A table has been dropped')
24
1/3/2019 9:29 AM DDL Triggers © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
25
DML with Output It is a shame that I need to do two things to find out what rows I UPDATE or DELETE… is there a better way?
26
DML with OUTPUT OUTPUT clause for DML
1/3/2019 9:29 AM DML with OUTPUT OUTPUT clause for DML Ability to return rows as part of DML operations Use “Inserted” and “Deleted” columns available to get pre- and post-update values Option to store returned rows OUTPUT… INTO… TABLE (orderId int) -- Update all 'unprocessed' to 'processed’ UPDATE Orders SET status='processed' OUTPUT INSERTED.orderId WHERE status='unprocessed' © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
27
Ranking I want to rank my data based on criteria … no, I don’t just mean order it…
28
Ranking Functions: Scenarios
1/3/2019 9:29 AM Ranking Functions: Scenarios Data analysis (RANK, DENSE_RANK, NTILE) Ability to generate ranks based on different criteria in same query Ability to separate presentation order from ranks Paging using ROW_NUMBER Common scenario for walking through result sets © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
29
EXAMPLE SELECT RANK()OVER(ORDER BY City)
as RANK, RANK()OVER(PARTITION BY City ORDER BY LastName) as PART_RANK, DENSE_RANK() OVER(ORDER BY City) as DENSE_RANK, ROW_NUMBER() OVER(ORDER BY City) as ROW_NUM, NTILE(4) OVER(ORDER BY City) as NTILE_4, LastName, FirstName, City FROM Employees ORDER BY City, LastName © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
30
DML with Output RANK 1/3/2019 9:29 AM
© 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
31
SNAPSHOT Isolation Hmmm …… I need more work to happen in parallel. How? OR I am migrating from Oracle and want to do as few changes as possible
32
Snapshot Isolation SQL Server 2000 Transaction isolation levels
1/3/2019 9:29 AM Snapshot Isolation SQL Server 2000 Transaction isolation levels Read Uncommitted Read Committed Repeatable Read Serializable SQL Server 2005 adds... Snapshot Two flavours: Statement = READ_COMMITTED Transaction = SERIALIZABLE © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
33
Snapshot Isolation Increased data availability for read applications
1/3/2019 9:29 AM Snapshot Isolation Increased data availability for read applications Allows non-blocking consistent reads in an OLTP environment Writers don’t block readers Readers don’t block writers Permits writes, which can cause conflicts BUT…includes mandatory conflict detection Snapshot Isolation trades: cost of concurrency (locking exclusion) for cost of CPU & I/O to construct transaction consistent view and read over versions © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
34
Snapshot Isolation: Scenarios
1/3/2019 9:29 AM Snapshot Isolation: Scenarios Reporting and ad-hoc queries running concurrently with OLTP Read-mostly database with relatively few writes Applications prone to deadlocks may behave better Consistent aggregates (e.g., AVG, SUM) Migration from versioning databases E.g. Oracle to SQL Server © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
35
I hate chunking data!!!! Please, tell me there is a better way…
Large data types I hate chunking data!!!! Please, tell me there is a better way…
36
…(max) Type Text/nText and Image have problems
1/3/2019 9:29 AM …(max) Type Text/nText and Image have problems Most varchar functions don’t work (e.g. Like) Not directly updateable Not allowed as procedure variables Extension to varchar, nvarchar, varbinary up to 2GB CREATE TABLE myTable (Id int, Picture varbinary(max)) © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
37
XML I have rich XML outside the db – and strings inside the db . Is there a better way?
38
XML Support in SQL 2005 SQL Server 2005 as an XML store
1/3/2019 9:29 AM XML Support in SQL 2005 SQL Server 2005 as an XML store Durable, recoverable, consistent Mature management tools Integration with existing relational data SQL Server 2005 as an XML source Accessible over HTTP and SOAP Exposes all data as XML Can be programmed just like a web service Why XML? Sparse, unstructured, hierarchical or recursive data Why store in SQL Server 2005? Transacted management, optimised queries, backup, interoperability, schema validation. © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
39
Native XML Store XML Data Type
1/3/2019 9:29 AM Native XML Store XML Data Type XML data type Native SQL type Use for column, variable or parameter CREATE TABLE docs (id INT PRIMARY KEY, xCol XML NOT NULL) Well-formed and validation checks Optional XML Schema enforcement Behaviours allow XQuery and extensions Exist, Query, Modify, Value © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
40
Native XML Store XML Index
1/3/2019 9:29 AM Native XML Store XML Index Create XML index on XML column CREATE PRIMARY XML INDEX idx_1 ON T (xCol) CREATE XML INDEX idx_2 ON T(xCol) USING XML INDEX idx_1 FOR PATH Creates indexes on structure (PATH), properties (PROPERTY) values (VALUE) Speeds up queries Entire query is optimised Same industry leading cost based optimiser Indexes are used as available 56 Speeds up queries on the tags and values © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
41
1/3/2019 9:29 AM XML © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
42
Summary T-SQL is alive and kicking
1/3/2019 9:29 AM Summary T-SQL is alive and kicking There is LOTS more than covered today Use T-SQL and CLR judiciously Links: Sample chapter “A First Look at SQL Server 2005 for Developers” Addison Wesley: Bob Beauchemin, Niels Berglund, Dan Sullivan Books Online - click on “Download Books Online” on right © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
43
© 2004 Microsoft Corporation. All rights reserved.
1/3/2019 9:29 AM © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
44
Code…
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.