ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Advanced TSQL

  SQL Server 2008
Agenda
ï‚—Enhancing DML Functionality
  ï‚—Output Clause
  ï‚—Merge Statement
ï‚—Managing Transactions
  ï‚—Implicit Transaction
  ï‚—Explicit Transaction
ï‚—Exception Handling
  Try…Catch
Agenda
ï‚—Extending SQL Server Functionality
  ï‚—CTE
  ï‚—XML
  ï‚—CLR
  ï‚—FileStream
  ï‚—Spatial
  ï‚—Full Text Search
  ï‚—Service Broker
Agenda
ï‚—Apply Operator
 ï‚—Cross Apply
 ï‚—Outer Apply
ï‚—Pivoting Data
 ï‚—PIVOT
 ï‚—UNPIVOT
ï‚—Ranking Functions
 ï‚—ROW_NUMBER
 ï‚—RANK
 ï‚—DENSE_RANK
 ï‚—NTILE
Output Clause
ï‚—Provides ability to access INSERTED and DELETED tables
  during DML statement execution as we can access in
  Triggers.
Output Example
Merge Statement
ï‚—Provides you the ability to compare rows between source
  and target tables, and then performs DML action accordingly
  only on target Table.
Merge Example
Managing Transaction
ï‚—Implicit Transaction
  ï‚—Setting mode for implicit transaction
     Manual Commit – Set Implicit_Transactions OFF
     Auto commit – Set Implicit_Transactions ON

ï‚—Explicit Transaction
  ï‚—Begin Transaction [Trans Name]
  ï‚—Commit Transaction [Trans Name]
  ï‚—Rollback Transaction [Trans Name]
  Savepoint – Save Transaction [Savepoint name]
Exception Handling (Try..Catch)
ï‚—Captures Error severity higher that 10
Try…Catch TSQL
Begin Try
       [SQL Statements]
End Try
Begin Catch
       [SQL Statements]
End Catch
Retrieving Error Information
ï‚— ERROR_NUMBER() returns the number of the error.


ï‚— ERROR_SEVERITY() returns the severity.


ï‚— ERROR_STATE() returns the error state number.


ï‚— ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.


ï‚— ERROR_LINE() returns the line number inside the routine that caused the error.


ï‚— ERROR_MESSAGE() returns the complete text of the error message. The text includes the values
   supplied for any substitutable parameters, such as lengths, object names, or times.
Exception Handling
CTE
ï‚— Like derived tables
ï‚— Iterates results sets
ï‚— Recursive

ï‚— CTE Parts
  ï‚— With Clause
  ï‚— CTE Name
  ï‚— Column Names
  ï‚— AS keyword
  ï‚— Anchor Query
  ï‚— Union All
  ï‚— Second Query
  ï‚— Outer Query
CTE
;
WITH cte(col1,col2..)
AS
(Anchor_Query
Union All
Second_Query)
Outer_Query;
PIVOTING DATA
ï‚—Moving rows for a column as columns for aggregate
 values, reversing that combination as well through
 unpivot

ï‚—Parts
 ï‚—Source Table
 ï‚—PIVOT/UNPIVOT Operator
 ï‚—Aggregation
 ï‚—Filter Clause (FOR)
 ï‚—Pivot Table
Pivot Syntax
From
  (Subquery) AS Source_Table
PIVOT (
Aggregate_Function(Column_ST)
FOR Column_ST IN (Values in Column_ST)
) AS Pivot_Table
Apply Operator
ï‚—Outer Apply
ï‚—Cross Apply

More Related Content

7a advanced tsql