SlideShare ist ein Scribd-Unternehmen logo
1 von 114
Downloaden Sie, um offline zu lesen
Security Classification
                       Business Classification




Fast Transition To SQL 2012 from
MSSQL 2005/2008 For Developers

                                                                          –
                                                                 cimid@rafael.co.il




         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                Business Classification



   Rafael Advanced Defense
           Systems
• Designs, develops, manufactures and supplies a wide range of high
  tech defense systems for air, land, sea and space applications.

• Sales in 2010 exceeding $1851M

• About 7000 Employees




                  Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                        Business Classification




    Speaker Qualifications
• SQL Server , Oracle , Sybase ,Sybase
  Anywhere - application & infrastructure
  DBA .
• 16 years of SQL experience, starting with
  version 6.5
• Education :
   – BSC (Information System Engineer)
   – MBA
   – Currently ME (System Engineering)
          Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                                Business Classification




   Previous Lectures-OOW 2010
RAFDOCS-#3545730-ORACLE -ORACLE OPEN WORLD 2010 - Rafael Mission-Critical Use of Oracle Active Data Guard/Oracle .NET
                                             Technology




                                  Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                 Business Classification



Previous Lectures-1




   Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                    Business Classification



  Previous Lectures-2
Database Mirror for the
   exceptional DBA
                                                                   –
                                                          cimid@rafael.co.il




      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                           Business Classification



       Previous Lectures-3

   When ODAC ,Oracle Sever
11GR2, Win HPC Server 2008 Come
                 together…
     David Itshak
     Rafael Advanced Defense System
     cimid@rafael.co.il




             Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
              Business Classification




         Previous Lectures-4
          DRP



shaked19@gmail.com



Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                 Business Classification



Previous Lectures-5




   Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification


                                  Agenda
• Using the old & new Analytic Functions in SQL 2012
   –   SQL Server 2005 and 2008 Ranking Functions Row_Number and Rank
   –   Ranking functions
   –   Inverse percentile functions
   –   Window functions
   –   Reporting functions
   –   Lag and lead functions
   –   Using the LAG and LEAD Functions
   –   First and last functions


• OVER Clause Support Enhanced




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification


                                   Agenda
• 14 New Functions and 1 Changed Function
   – Type Conversion function : From cast & convert to :
       •   PARSE
       •   TRY_CONVERT
       •   TRY_PARSE
   – New Logical Functions : IIF and CHOOSE.
   – New Date and Time Functions.
   – String functions : CONCAT & FORMAT


• Overview of WITH RESULT SETS Feature




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                  Business Classification


                              Agenda
• From RAISERROR to THROW statement to raise an
  exception



• Moving From Identity Column to Sequence Number .



• What Next I Expect in Next Version of SQL Server under
  the slides with “In Next version maybe”




                    Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification


                     Next Lecture
• Ad-hoc Query Paging Implementation
   – The old way stored procedures to page large tables or queries
   – OFFST/FTECH Filter in SQL 2012


• New DMVs to get configuration, installation and memory
  dump information




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


                  Next Lecture
• Contained Database - The end of Orphan users .

• From FileStream to FileTables

• Power shell : From Backup with SMO to Restore-
  SqlDatabase cmdlet

• SQL 2012 Spatial




                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification


             Exercise material
            Based on original Material !!!

Programmability Enhancements (Database Engine) SQL 2012 , Demo
   script

Script Name : fast_transition_to_mssql_2012.sql
Author:Dadid Itshak
Email:cimid@rafael.co.il
Date:Feb 6th 2012

Content :
Modification History:
Date       Who            What




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                            Business Classification



           NEW Analytic Functions
• The following analytic functions have been
  added to SQL Server 2012.
  – CUME_DIST
  – LAST_VALUE
  – PERCENTILE_DISC
  – FIRST_VALUE
  – LEAD
  – PERCENT_RANK
  – LAG


              Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                             Business Classification



        OVER Clause Support Enhanced
• The OVER clause has been extended to
  support window functions.
• Window functions perform a calculation
  across a set of rows that are in some
  relationship to the current row.
  – use the ROWS or RANGE clause over a set of
    rows to calculate a moving average or cumulative
    total.)
• Ordering rows within a partition is now
  supported in the aggregate functions that
  allow the OVER clause to be specified.
               Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                                Business Classification



         NEW Analytic Functions and OVER Clause
                   Support Enhanced
Source : Window Functions in SQL Server: Part 2-The Frame ,24 November 2011
by Fabiano Amori , Simple-Talk.com




                                  Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



                 Using the Analytic Functions
• DB has many built-in analytic functions that enable you to perform
  complex calculations, such as finding the top-selling product type for
  each month, the top salespersons, and so on.

• Analytic functions are organized into the following categories:

    – Ranking functions enable you to calculate ranks, percentiles, and n-
      tiles (tertiles, quartiles, and so on).
    – Window functions enable you to calculate cumulative and moving
      aggregates.
    – Lag and lead functions enable you to get a value in a row where that
      row is a certain number of rows away from the current row.
    – First and last functions enable you to get the first and last values in
      an ordered group.


                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification



            Using the Analytic Functions 1
• In Next version maybe :


   – Inverse percentile functions enable you to calculate the
     value that corresponds to a percentile.

   – Hypothetical rank and distribution functions enable you to
     calculate the rank and
   – percentile that a new row would have if you inserted it into
     a table.

   – Linear regression functions enable you to fit an ordinary-
     least-squares regression line to a set of number pairs.

                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                           Business Classification



               Analytic Functions Description

USE TempDB
GO
IF OBJECT_ID('Tab1') IS NOT NULL
DROP TABLE Tab1
GO
CREATE TABLE Tab1 (Col1 INT)
GO

INSERT INTO Tab1 VALUES(5), (5), (3) , (1)
GO




                             Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



           Analytic Functions Description - 1
• RANK() :
   – Returns the rank of items in a group.
   – leaves a gap in the sequence of rankings in the event of a tie.
• DENSE_RANK():
   – Returns the rank of items in a group.
   – Doesn’t leave a gap in the sequence of rankings in the event of a tie.

    SELECT Col1,
    RANK() OVER(ORDER BY Col1 DESC) AS "RANK()" FROM Tab1
    GO
    SELECT Col1,
    DENSE_RANK() OVER(ORDER BY Col1 DESC) AS "DENSE_RANK" FROM Tab1




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification



               Analytic Functions Description - 2
• CUME_DIST() :
   – Short for cumulative distribution.
   – Returns the position of a specified value relative to a group of values.

If X is a purely discrete random variable, then it attains values x1, x2, ...
with probability pi = P(xi)




Cumulative distribution function of a discrete probability distribution



Properties :


                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



             Analytic Functions Description - 3

• PERCENT_RANK():
   – Returns the percent rank of a value relative to a group of values.

• NTILE() Returns n-tiles: tertiles, quartiles, and so on.
SELECT Col1,
NTILE(3) OVER(ORDER BY Col1 DESC) AS "NTILE(3)"
FROM Tab1




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                               Business Classification



          Analytic Functions Description - 4

• ROW_NUMBER() Returns a number with each row in a group
SELECT Col1,
ROW_NUMBER() OVER(ORDER BY Col1 DESC) AS "ROW_NUMBER()"
FROM Tab1




                 Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                        Business Classification


     Using the Analytic Functions -Example Table

/*
The Example Table
The total_sales table stores the sum of all the sales by dollar
amount for a particular year, month, product type, and employee.
*/


CREATE TABLE total_sales (
  year INTEGER NOT NULL,
  month INTEGER NOT NULL,
  prd_type_id INTEGER,
  emp_id INTEGER,
  amount NUMERIC,
  CONSTRAINT total_sales_pk PRIMARY KEY (
    year, month, prd_type_id, emp_id
  );




                          Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification


   Using the Analytic Functions -Example Table
/*
total_sales table contains five columns, as follows:
YEAR stores the year the sales took place.
MONTH stores the month the sales took place (1 to 12).
PRD_TYPE_ID stores the product_type_id of the product.
EMP_ID stores the employee_id of the employee who handled the sales.
AMOUNT stores the total dollar amount of the sales.

The following query retrieves the first 12 rows from the total_sales table:
*/
select top (12) * from [dbo].[total_sales]




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



    Using the RANK() and DENSE_RANK() Functions
• You use RANK() and DENSE_RANK() to rank items in a group.

• The difference :
   – RANK() leaves a gap in the sequence when there is a tie
   – DENSE_RANK() leaves no gaps.

• Example . if you were ranking sales by product type and two product
  types tie for first place

   – RANK() would put the two product types in first place, but the next
     product type would be in third place.
   – DENSE_RANK() would also put the wo product types in first place,
     but the next product type would be in second place.



                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



Using the RANK() and DENSE_RANK() Functions -1
SELECT
prd_type_id, SUM(amount),
RANK() OVER (ORDER BY SUM(amount) DESC) AS rank,
DENSE_RANK() OVER (ORDER BY SUM(amount) DESC) AS dense_rank
FROM total_sales
WHERE year = 2012
AND amount IS NOT NULL
GROUP BY prd_type_id
ORDER BY prd_type_id;




• Notice :
   – No ties => RANK() and DENSE_RANK() return the same ranks.



                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


Controlling Ranking of Null Values - Next version maybe
• Using the NULLS FIRST and NULLS LAST Clauses When using an
   analytic function
• Explicitly control whether nulls are the highest or lowest in a group .
• Example : Use NULLS LAST to specify that nulls are the lowest:
SELECT
prd_type_id, SUM(amount),
RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST) AS rank,
DENSE_RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST)
AS dense_rank
FROM total_sales
WHERE year = 2012
GROUP BY prd_type_id
ORDER BY prd_type_id;



                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                  Business Classification


Using the PARTITION BY Clause with Analytic Functions
• When you need to divide the groups into subgroups.
• Example : Subdivide the sales amount by month, use PARTITION BY
   month as follows :
SELECT
prd_type_id, month, SUM(amount) AS SUM_Amount ,
RANK() OVER (PARTITION BY month ORDER BY SUM(amount) DESC)
    AS rank
FROM total_sales
WHERE year = 2012
AND amount IS NOT NULL
GROUP BY prd_type_id, month
ORDER BY prd_type_id, month;




                    Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


                 Using ROLLUP with Analytic Function

• Use ROLLUP and RANK() to get the sales rankings by product type ID:
SELECT
prd_type_id, SUM(amount) SUM_Amount,
RANK() OVER (ORDER BY SUM(amount) DESC) AS rank
FROM total_sales
WHERE year = 2012
GROUP BY ROLLUP(prd_type_id)
ORDER BY prd_type_id;




                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


             Using CUBE and RANK() with Analytic Function

• Use CUBE and RANK() to get all rankings of sales by product type ID an
   employee ID :
SELECT
prd_type_id, emp_id, SUM(amount)as SUM_Amount,
RANK() OVER (ORDER BY SUM(amount) DESC) AS rank
FROM total_sales
WHERE year = 2012
GROUP BY CUBE(prd_type_id, emp_id)
ORDER BY prd_type_id, emp_id;




                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


      Using GROUPING SETS and RANK() with Analytic Function

• Uses GROUPING SETS and RANK() to get just the sales amount
   subtotal
• GROUPING SETS gets just the subtotal rows. In following example
subtotals for product type id and employee id .
SELECT
prd_type_id, emp_id, SUM(amount) SUM_Amount,
RANK() OVER (ORDER BY SUM(amount) DESC) AS rank
FROM total_sales
WHERE year = 2012
GROUP BY GROUPING SETS(prd_type_id, emp_id)
ORDER BY prd_type_id, emp_id




                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


        Using the CUME_DIST() and PERCENT_RANK() Functions
• CUME_DIST() to calculate the position of a specified value relative to a
   group of values.
• CUME_DIST() is short for cumulative distribution.
• PERCENT_RANK() to calculate the percent rank of a value relative to a
   group of values.
• Example : Use of CUME_DIST() and PERCENT_RANK() to get the
   cumulative distribution and percent rank of sales:
SELECT
prd_type_id, SUM(amount) SUM_Amount,
CUME_DIST() OVER (ORDER BY SUM(amount) ASC) AS cume_dist,
PERCENT_RANK() OVER (ORDER BY SUM(amount) ASC) AS
    percent_rank
FROM total_sales
WHERE year = 2012
GROUP BY prd_type_id
ORDER BY prd_type_id
                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                 Business Classification


     Using the CUME_DIST() and PERCENT_RANK() Functions -
SELECT
prd_type_id, SUM(amount) SUM_Amount,
CUME_DIST() OVER (ORDER BY SUM(amount) ASC) AS cume_dist,
PERCENT_RANK() OVER (ORDER BY SUM(amount) ASC) AS
   percent_rank
FROM total_sales
WHERE year = 2012
GROUP BY prd_type_id
ORDER BY prd_type_id




                   Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                         Business Classification


                            Using the NTILE() Function
• You use NTILE(buckets) to calculate n-tiles (tertiles, quartiles, and so
  on);
    – buckets specifies the number of “buckets” into which groups of rows are placed.
•  Example : 4 is passed to NTILE() to split the groups of rows into four
   buckets
SELECT
prd_type_id, SUM(amount) SUM_Amount,
NTILE(4) OVER (ORDER BY SUM(amount) DESC) AS ntile
FROM total_sales
WHERE year = 2012
AND amount IS NOT NULL
GROUP BY prd_type_id
ORDER BY prd_type_id;




                           Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                 Business Classification


           Using the ROW_NUMBER() Function

• You use ROW_NUMBER() to return a number with each row in a
   group, starting at 1. Example :
SELECT
prd_type_id, SUM(amount) SUM_Amount,
ROW_NUMBER() OVER (ORDER BY SUM(amount) DESC) AS
    row_number
FROM total_sales
WHERE year = 2012
GROUP BY prd_type_id
ORDER BY prd_type_id;




                   Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


        Use the inverse percentile functions -Next version maybe
• 2 inverse percentile functions: PERCENTILE_DISC(x)and
  PERCENTILE_CONT(x).
• Reverse of CUME_DIST() and PERCENT_RANK().
• PERCENTILE_DISC(x) examines the cumulative distribution values in
  each group until it finds one that is greater than or equal to x.
• PERCENTILE_CONT(x) examines the percent rank values in each group
  until it finds one that is greater than or equal to x.
• Example how to get the sum of the amount whose percentile is greater
  than or equal to 0.6
SELECT
PERCENTILE_CONT(0.6) WITHIN GROUP (ORDER BY SUM(amount) DESC)
AS percentile_cont,
PERCENTILE_DISC(0.6) WITHIN GROUP (ORDER BY SUM(amount) DESC)
AS percentile_disc
FROM total_sales
WHERE year = 2012
GROUP BY prd_type_id;
                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


                       Using the Window Function
• Calculate things like cumulative sums and moving averages within a
  specified range of rows, a range of values, or an interval of time.

• “window” describes a subset of rows within the result set.

• The subset of rows in window is processed by the window functions,
  which return a value.

• You can define the start and end of the window.




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


             Using the Window Function-WINDOWS FRAME
• OVER (
     [ <PARTITION BY clause> ]
     [ <ORDER BY clause> ]
     [ <ROW or RANGE clause> ]
    )

• “window” describes a subset of rows within the result set.
• The subset of rows in window is processed by the window functions,
  which return a value.
• You can define the start and end of the window.
• windows correspond to each distinct CustomerID :




                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                           Business Classification


                 Using the Window Function-WINDOWS FRAME
[ROWS | RANGE] BETWEEN <Start expr> AND <End expr>
Where:
<Start expr> is one of:
    – UNBOUNDED PRECEDING: The window starts in the first row of the partition
    – CURRENT ROW: The window starts in the current row
    – <unsigned integer literal> PRECEDING or FOLLOWING
<End expr> is one of:
    – UNBOUNDED FOLLOWING: The window ends in the last row of the partition
    – CURRENT ROW: The window ends in the current row
    – <unsigned integer literal> PRECEDING or FOLLOWING


• Where it is not explicitly specified, the default window frame is “range
  between unbounded preceding and current row”,
    – the top row in the window is the first row in the current partition, and the bottom row in
      the window is the current row.




                             Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


               Performing a Cumulative Sum
• Example : a cumulative sum to compute the cumulative sales amount for
  2012, from January to December;
• Notice : each monthly sales amount is added to the cumulative amount
  that grows after each month:
SELECT
month, SUM(amount) AS month_amount,
SUM(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND
   CURRENT ROW)
AS cumulative_amount
FROM total_sales
WHERE year = 2012
GROUP BY month
ORDER BY month;

                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


    Performing a Cumulative Sum- How It Works ? - 1
• Computes the cumulative total of the sales amounts, starting at month 1,
  then month1 + month 2, then month1 + month 2 + month 3, and so on,
  up to and including month 12.
SUM(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND
  CURRENT ROW)
AS cumulative_amount
• SUM(amount) computes the sum of an amount.
• The outer SUM() computes the cumulative amount.
• ORDER BY month orders the rows read by the query by month.
• ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  defines the start and end of the window.
• The start is set to UNBOUNDED PRECEDING- the start of the window
  is fixed at the first row in the result set returned by the query.


                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                           Business Classification


    Performing a Cumulative Sum- How It Works ? - 2
• The end of the window is set to CURRENT ROW;

• CURRENT ROW- represents the current row in the result set being
  processed, and the end of the window slides down one row after the
  outer SUM() function computes and returns the current cumulative
  amount.

• The start of the window is fixed at month 1, but the bottom of the window
  moves down one row in the result set after each month’s sales amounts
  are added to the cumulative total.

• This continues until the last row in the result set is processed by the
  window and the SUM() functions.
    – (i.e., the sum of the sales amount for that month is added to the cumulative total).



                             Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                           Business Classification


    Performing a Cumulative Sum- How It Works ? - 3
• Don’t confuse the end of the window with the end of the result set. In the
  previous example, the end of the window slides down one row in the
  result set as each row is processed
    – (i.e., the sum of the sales amount for that month is added to the cumulative total).


• In the example, the end of the window starts at the first row, the sum
  sales amount for that month is added to the cumulative total, and then
  the end of the window moves down one row to the second row. At this
  point, the window sees two rows.

• The sum of the sales amount for that month is added to the cumulative
  total, and the end of the window moves down one row to the third row. At
  this point, the window sees three rows. This continues until the twelfth
  row is processed. At this point, the window sees twelve rows.



                             Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


          Performing a Cumulative Sum - CONT
•  Example uses a cumulative sum to compute the cumulative sales
   amount, start with June of 2012 (month 6) and ending in December of
   2012 (month 12
SELECT
month, SUM(amount) AS month_amount,
SUM(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND
    CURRENT ROW) AS
cumulative_amount
FROM total_sales
WHERE year = 2012
AND month BETWEEN 6 AND 12
GROUP BY month
ORDER BY month;
 Ex : month 1 cumulative_amount =(160221.98+2972073.67)=1132295.6
                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                          Business Classification


                    Performing a Moving Average
• Example : Moving average of the sales amount between the current
month and the previous 3 months:
SELECT
month, SUM(amount) AS month_amount,
AVG(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN 3 PRECEDING AND CURRENT
   ROW)
AS moving_average
FROM total_sales
WHERE year = 2012
GROUP BY month
ORDER BY month;
Example :
month 11 moving_average= 209262.032500= (160221.98+264480.79+199609.68+212735.68)/4
month 2 moving_average= 106098.575000= (116671.60+95525.55)/2

                            Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                          Business Classification


   Performing a Moving Average- How It Works ? - 1
• The query uses the following expression to compute the moving
  average:
AVG(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN 3 PRECEDING AND CURRENT
  ROW) AS moving_average

• SUM(amount) computes the sum of an amount.
• The outer AVG() computes the average
• ORDER BY month orders the rows read by the query by month.

• ROWS BETWEEN 3 PRECEDING AND CURRENT ROW :
   –   Defines the start of the window as including the three rows preceding the current row;
   –   The end of the window is the current row being processed.
   –   Entire expression computes the moving average of the sales amount between the
   –   current month and the previous three months.

                            Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


    Performing a Moving Average- How It Works ? - 2
• For first 2 months less than 3 months , moving average is based on
  only the months available.

• Both the start and the end of the window begin at row #1 read by the
  query.

• The end of the window moves down after each row is processed.

• The start of the window moves down only after row no 4 has been
  processed, and subsequently moves down one row after each row is
  processed . Until the last row in the result .




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                          Business Classification


                   Performing a Centered Average
• Example : Computes the moving average of the sales amount centered
  between the previous and next month from the current month:
SELECT
month, SUM(amount) AS month_amount,
AVG(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1
   FOLLOWING)
AS moving_average
FROM total_sales
WHERE year = 2012
GROUP BY month
ORDER BY month;
Example :
month 5 moving average=(175998.80+154349.44+124951.36)/3=151766.533333
month 1 moving average=(95525.55+116671.60)/2 =106098.575000

                            Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification


     Performing a Centered Average-How It Works ? - 1
AVG(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1
  FOLLOWING) AS moving_average

•   SUM(amount) computes the sum of an amount.
•   The outer AVG() computes the average.
•   ORDER BY month orders the rows read by the query by month.
•   ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING defines the
    start of the window as including the row preceding the current row being
    processed.

• The end of the window is the row following the current row.

• The entire expression computes the moving average of the sales
  amount between the current month and the previous month.
                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification


    Performing a Centered Average-How It Works ? - 2
• Because for the first and last month less than the full three months of
  data are available, the moving average is based on only the months
  available.

• The start of the window begins at row No 1 read by the query.

• The end of the window begins at row No 2 and moves down after each
  row is processed.

• The start of the window moves down only once row #2 has been
  processed.

• Processing continues until the last row read by the query is processed.



                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


Getting the First and Last Rows Using FIRST_VALUE() and LAST_VALUE()

• Use the FIRST_VALUE() and LAST_VALUE() functions to get the first
  and last rows in a window.
• Example : use FIRST_VALUE() and LAST_VALUE() to get the previous
  and next month’s sales amount
SELECT
month, SUM(amount) AS month_amount,
FIRST_VALUE(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
AS previous_month_amount,
LAST_VALUE(SUM(amount)) OVER
(ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
AS next_month_amount
FROM total_sales
WHERE year = 2012
GROUP BY month
ORDER BY month;


                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


                      Using the Reporting Function
• You use the reporting functions to perform calculations across groups
  and partitions within groups.
• You can perform reporting with the following functions: SUM(), AVG(),
  MAX(), MIN(),COUNT(), VARIANCE(), and STDDEV().
• For the first three months of 2012, the following query reports
• Total sum of all sales for all three months (total_month_amount).
• Total sum of all sales for all product types ( total_product_type_amount).
SELECT
month, prd_type_id,
SUM(SUM(amount)) OVER (PARTITION BY month)
AS total_month_amount,
SUM(SUM(amount)) OVER (PARTITION BY prd_type_id)
AS total_product_type_amount
FROM total_sales
WHERE year = 2012
AND month <= 3
GROUP BY month, prd_type_id
ORDER BY month, prd_type_id;
                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


                 Using the LAG() and LEAD() Functions
• You use the LAG() and LEAD() functions to get a value in a row where
  that row is a certain number of rows away from the current row.
• The following query uses LAG() and LEAD() to get the previous and next
  month’s sales amount:

SELECT
month, SUM(amount) AS month_amount,
LAG(SUM(amount), 1) OVER (ORDER BY month) AS previous_month_amount,
LEAD(SUM(amount), 1) OVER (ORDER BY month) AS next_month_amount
FROM total_sales
WHERE year = 2012
GROUP BY month
ORDER BY month;




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


   Using the LAG() and LEAD() Functions- It Works ?
• LAG(SUM(amount), 1) gets the previous row’s sum of the amount.

• LEAD(SUM(amount),1) gets the next row’s sum of the amount.




                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification


Using the Hypothetical Rank and Distribution Functions- Next version maybe ?
  • Use the hypothetical rank and distribution functions to calculate the rank
    and percentile that a new row would have if you inserted it into a table.
  • Perform hypothetical calculations with the following functions: RANK(),
    DENSE_RANK(), PERCENT_RANK(), and CUME_DIST() to get the rank
    and percent rank of sales by product type for 2012:
  SELECT
  prd_type_id, SUM(amount),
  RANK() OVER (ORDER BY SUM(amount) DESC) AS rank,
  PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank
  FROM all_sales
  WHERE year = 2003
  AND amount IS NOT NULL
  GROUP BY prd_type_id
  ORDER BY prd_type_id




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification


Using the Hypothetical Rank and Distribution Functions- Next version maybe ?
  • Use the hypothetical rank and distribution functions to calculate the rank
    and percentile that a new row would have if you inserted it into a table.
  • Perform hypothetical calculations with the following functions: RANK(),
    DENSE_RANK(), PERCENT_RANK(), and CUME_DIST() to get the rank
    and percent rank of sales by product type for 2012:
  SELECT
  prd_type_id, SUM(amount),
  RANK() OVER (ORDER BY SUM(amount) DESC) AS rank,
  PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank
  FROM all_sales
  WHERE year = 2003
  AND amount IS NOT NULL
  GROUP BY prd_type_id
  ORDER BY prd_type_id




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification


Using the Hypothetical Rank and Distribution Functions- Next version maybe ? 1


  • The next query shows the hypothetical rank and percent rank of a sales
    amount of $500,000:

  SELECT
  RANK(500000) WITHIN GROUP (ORDER BY SUM(amount) DESC)
  AS rank,
  PERCENT_RANK(500000) WITHIN GROUP (ORDER BY SUM(amount) DESC)
  AS percent_rank
  FROM all_sales
                              RANK         PERCENT_RANK
  WHERE year = 2003                               0.25
  AND amount IS NOT NULL
  GROUP BY prd_type_id
  ORDER BY prd_type_id;




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                             Business Classification



   14 New Functions and 1 Changed Function


• New Type Conversion function :
  – PARSE
  – TRY_CONVERT
  – TRY_PARSE
• New Logical Functions : IIF and CHOOSE.
• New Date and Time Functions.
• String functions : CONCAT & FORMAT



               Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



               PARSE Conversion Function

• The PARSE function converts any string value to a Numeric or to a Date
  and Time format.

•   For general type conversions, continue to use CAST or CONVERT.

•   Performance overhead in parsing the string value.

• PARSE relies on the presence of .the .NET Framework Common
  Language Runtime (CLR).
• Supports an optional USING clause indicating the culture.
• The culture is any valid culture supported by the .NET framework.
• If not specified ,uses the current session’s effective language


                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



            PARSE Conversion Function- 1

/* Using PARSE Function to Convert String to Date Time */
SELECT PARSE('2/06/2012' AS datetime) AS [Using PARSE Function]
GO
Output:
Using PARSE Function
2012-02-06 00:00:00.000



/* PARSE with implicit setting of language
The English language is mapped to en-US specific culture
*/
SET LANGUAGE 'English'
SELECT PARSE('02/06/2012' AS datetime2) AS Result

Output:
Result
2012-02-06 00:00:00.0000000


                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                         Business Classification



              PARSE Conversion Function- 2
/* Using PARSE Function to Convert String to INT */
SELECT TRY_PARSE('ONE' AS INT) AS [Using TRY_PARSE Function]


Output :
Using TRY_PARSE Function
NULL



/* Using PARSE Function to Convert String to Date Time */
SELECT TRY_PARSE('02/06/2012' AS datetime) AS [Using TRY_PARSE Function]
GO

Output :
Using TRY_PARSE Function
2012-02-06 00:00:00.000




                           Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



            PARSE Conversion Function- 3

/*Detecting nulls with TRY_PARSE */
SELECT
    CASE WHEN TRY_PARSE('Friday, 88 December 2011' AS datetime USING 'en-US')
     IS NULL
        THEN 'True'
        ELSE 'False'
END
AS Result

Output :

Result
True




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



            TRY_PARSE Conversion Function

• TRY_PARSE to PARSE is like TRY_CONVERT is to CONVERT

• Convert any string value to a Numeric or to a Date and Time format.

• if cannot convert a passed value to Numeric or to a Date and Time
  format, then it will return a NULL value rather than failing the entire TSQL
  code




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                         Business Classification



        TRY_PARSE Conversion Function- 1


 /* Using PARSE Function to Convert String to INT */
SELECT TRY_PARSE('ONE' AS INT) AS [Using TRY_PARSE Function]

Output :
Using TRY_PARSE Function
NULL


/* Using PARSE Function to Convert String to Date Time */
SELECT TRY_PARSE('02/06/2012' AS datetime) AS [Using TRY_PARSE Function]
GO

Output :
Using TRY_PARSE Function
2012-02-06 00:00:00.000



                           Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                        Business Classification



           TRY_PARSE Conversion Function- 2


/*Detecting nulls with TRY_PARSE */
SELECT
    CASE WHEN TRY_PARSE('Friday, 88 December 2011' AS datetime USING 'en-US')
        IS NULL
        THEN 'True'
        ELSE 'False'
END
AS Result

Output :

Result
True




                          Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification



         TRY_CONVERT Conversion Function
• Takes the values passed to it and tries to convert it to a specified Data
  Type.
• When the input value isn’t convertible, instead of generating an error (like
  CONVERT ) , returns a NULL
• If conversion is successful then it will return the value as the specified
  data type. Otherwise it will return a NULL value.
• If conversion that is explicitly not permitted, then TRY_CONVERT fails
  with an error.

• Useful is in dynamic schema scenarios:
    – WHERE datatype = 'INT' AND CAST(val AS INT) > 10
• With CAST/CONVER can fail on a conversion error because SQL
  Server may try conversion before evaluating the left expression.
• With TRY_CONVERT avoid such failures as follows :
    – WHERE datatype = 'INT' AND TRY_CONVERT(INT,val) > 10


                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification



    TRY_CONVERT Conversion Function- 1
/* CONVERT returns error */
SELECT CONVERT(float,'test')
/*
Output :
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.


/* TRY_CONVERT returns null */
 SELECT
    CASE WHEN TRY_CONVERT(float,'test') IS NULL
    THEN 'Cast failed'
    ELSE 'Cast succeeded'
END AS Result;
GO

output:

Result
------------
Cast failed

                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



     TRY_CONVERT Conversion Function- 2

SELECT TRY_CONVERT(INT,289) AS result1, TRY_CONVERT(INT, 'Kuki') AS             result2;
output:

Result1 result2
289     NULL



/* TRY_CONVERT fails with an error
   Because an integer cannot be cast into an xml data type
*/
 SELECT TRY_CONVERT(xml, 4) AS Result;

Output is error:
Msg 529, Level 16, State 2, Line 13
Explicit conversion from data type int to xml is not allowed.




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                        Business Classification



         TRY_CONVERT Conversion Function- 3

/*
  TRY_CONVERT succeeds.
  This example demonstrates that the expression must be in the expected format.
 */
SET DATEFORMAT mdy;
SELECT TRY_CONVERT(datetime2, '12/31/2010') AS Result;
GO

output

Result
----------------------------------
2010-12-31 00:00:00.0000000

(1 row(s) affected)




                          Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification



                 CHOOSE Logical Function
• Return the item at the specified index from the list of values which are
  available.
• In this example we have 3 values and we are asking to select the second
  value in the list which is "SEQUENCE".

SELECT 'New SQL Server Release feature' =
CHOOSE(2, 'RESULT SETS',
'SEQUENCE','WINDOWS FUNCTION')




                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification



                        IIF Logical Function
• Syntax
   – IIF ( boolean_expression, true_value, false_value ).
• IIF is a shorthand way for writing a CASE statement.
• Since CASE statements can be nested only up to the level of 10, IIF
  statements can also be nested only up to the maximum level of 10

/*A. Simple IIF example*/

DECLARE @a int = 2;
DECLARE @b int = 40;
SELECT IIF ( @a > @b, 'TRUE', 'FALSE' ) AS Result;




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                  Business Classification



                  IIF Logical Function-1
/* B. IIF with NULL constants */
SELECT IIF ( 45 > 30, NULL, NULL ) AS Result




 /* C. IIF with NULL parameters*/
 DECLARE @P INT = NULL;
 DECLARE @S INT = NULL;
 SELECT IIF ( 45 > 30, @p, @s ) AS
 Result;




                    Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                Business Classification



                IIF Logical Function-2
/*
Let’s execute the below TSQL code which will use the IIF
function to return the results.
The logic is if the product Code is between 0 and 3 then
return Mechanics Part otherwise return Electronic Part.
*/
SELECT prd_type_id ,
  IIF(prd_type_id Between 1 AND 3 ,'Mechanics
Part','Electronic Part') AS Part_Type
FROM [dbo].[total_sales]




                  Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                                Business Classification



                            New FORMAT Function
•   The FORMAT function, returns a returns a value formatted with the specified
    format and optional culture
•   Syntax
     –   FORMAT ( value, format [, culture ] )
•   The format argument must contain a valid .NET Framework format string:
     –   Standard format string (for example, "C" or "D")
     – or a pattern of custom characters for dates and numeric values (for example,
       "MMMM dd, yyyy (dddd)").

•   culture Optional nvarchar argument specifying a culture.

•   If the culture argument is not provided, then the language of the current session
    is used.
•   This language is set either implicitly, or explicitly by using the SET LANGUAGE
    statement.
•   culture accepts any culture supported by the .NET Framework as an argument;


                                  Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



                New FORMAT Function- 1
DECLARE @ProductCost INT = 261;
SELECT FORMAT ( @ProductCost, 'c', 'en-IN' ) AS [Using 'en-IN' Culture];
SELECT FORMAT ( @ProductCost, 'c', 'de-DE' ) AS [Using 'de-DE' Culture];
GO
output:

Using 'en-IN' Culture
Rs. 261.00

Using 'de-DE' Culture
261,00 €




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification



                 New FORMAT Function- 2

DECLARE @TodaysDate DATETIME = GETDATE()
SELECT FORMAT ( @TodaysDate, 'd', 'en-US' ) AS [Using 'en-US' Culture]
SELECT FORMAT ( @TodaysDate, 'd', 'de-DE' ) AS [Using 'de-DE' Culture]
GO
output:

Using 'en-US' Culture
2/3/2012

Using 'de-DE' Culture
03.02.2012




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                          Business Classification



                       New CONCAT Function
•   Returns a string that is the result of concatenating two or more string values .

•   Implicitly coverts all arguments to string types and then concatenate the inputs.

•   Requires a minimum of two input values else the concatenation fails.

•   If all the arguments are null, an empty string of type varchar(1) is returned

•   The return type depends on the type of the arguments. The following table
    illustrates the mapping.




                            Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                         Business Classification



                 New CONCAT Function- 1

/*Use CONCAT function with a PRINT statement*/
SET NOCOUNT ON
SELECT CONCAT('Lecture on MSSQL 2012 By David Itshak                               ', GETDATE()) as result
GO
SET NOCOUNT OFF

output:
result
Lecture on MSSQL 2012 By   David Itshak               Feb      3 2012         1:35AM




                           Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                         Business Classification



                     New CONCAT Function- 2

/*   Using CONCAT with NULL values */

 CREATE TABLE #temp (
    emp_name nvarchar(200) NOT NULL,
    emp_middlename nvarchar(200) NULL,
    emp_lastname nvarchar(200) NOT NULL
);
INSERT INTO #temp VALUES( 'Name', NULL, 'Lastname' );
SELECT CONCAT( emp_name, emp_middlename, emp_lastname ) AS Result
FROM #temp;
drop table #temp;
GO
 Output :

Result
------------------
NameLastname




                           Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



              New Date and Time Functions
•   DATEFROMPARTS Function

•   TIMEFROMPARTS Function

•   DATETIMEFROMPARTS Function

•   DATETIME2FROMPARTS Function

•   SMALLDATETIMEFROMPARTS Function

•   DATETIMEOFFSETFROMPARTS Function

•   EOMONTH Function




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                          Business Classification



              Using TIMEFROMPARTS Function
•   Returns a full time value .
•   SYNTAX:
     TIMEFROMPARTS (HOUR, MINUTE, SECONDS, FRACTIONS, PRECISION)
•   Fractions argument depends on the precision argument.

•   Examples:

     – When fractions= 5 and precision= 1, then fractions = 5/10^1 of a second.
     – When fractions =50 and precision=2, then fractions=50/10^2=50/100 of a
       second.
     – When fractions=500 and precision=3, then fractions=500/10^3=500/1000 of
       a second.




                            Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification



           Using TIMEFROMPARTS Function-1
DECLARE
  @HOUR    INT = 12
, @MINUTE INT = 37
, @SECONDS INT = 42

SELECT
 TIMEFROMPARTS (@HOUR, @MINUTE, @SECONDS, 500, 3)
 AS [Result Using TIMEFROMPARTS Function]
GO

Output :

Result Using TIMEFROMPARTS Function
====================================
12:37:42.500


                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


  EXECUTE Statement WITH RESULT SETS argument
• The EXECUTE statement can now specify the metadata returned from
  the statement by using the WITH RESULT SETS argument.

• A limitation in the previous versions of SQL Server that whenever you
  wanted to change a Column Name or a Data Type within the result set of
  a Stored Procedure you ended up making changes to all the references
  within a Stored Procedure.

• This feature can be very useful when executing a stored procedure in
  SSIS where you can execute the Stored Procedure with the required
  columns names and appropriate data types.




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification


    EXECUTE Statement WITH RESULT SETS argument-
• Example : Using EXECUTE to redefine a two result sets

• When executing a statement that returns more than one result set, define
  each expected result set. The following example 2 creates a procedure
  that returns two result sets. Then the procedure is executed using the
  WITH RESULT SETS clause, and specifying two result set definitions

•     The first result set doesn't change the names or data types and the
     second result set we have changed the column names and data types
     using this new feature.




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


      EXECUTE Statement WITH RESULT SETS argument-2
USE tempdb
GO

IF EXISTS (SELECT * FROM sys.objects WHER
object_id = OBJECT_ID(N'[dbo].[UsingWithResultSets]') AND
type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UsingWithResultSets]
GO
CREATE PROCEDURE UsingWithResultSets
AS
BEGIN
 SELECT top(2)
 job_id ,
  CONCAT([first_name] , ' ' , [last_name]) AS Name,
   [division_id] FROM [dbo].[employees];
   SELECT top(2)
  [prd_type_id],
  CONCAT( [year] ,'-', [month]) Q,
   [amount] FROM [dbo].[total_sales]

END
GO
                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


 EXECUTE Statement WITH RESULT SETS argument-3
/* Execute Stored Procedure */
EXEC UsingWithResultSets
GO
/* Using WITH Result Sets Feature Of SQL Server 2012 */


EXEC UsingWithResultSets
WITH RESULT SETS
(
 (
 job_id VARCHAR(5),
 employeen_Name VARCHAR(200),
  division_id VARCHAR(10)
 ) ,
  (
 prd_type_idntity int,
 quar VARCHAR(50),
  amount_of_prod numeric(9,3)
 )
)

                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                              Business Classification



              Legacy Error Handling
• In SQL Server 2000 @@error system function.
• An @@error value of 0 means no error occurred.
• limitations : automatically reset to 0 after every
  successful statement.
• you cannot have any statements between the code
  that you expect might produce an exception and the
  code that checks the value of @@error.




                Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification



                Legacy Error Handling- 1
USE [tempdb]
GO
IF OBJECT_ID('dbo.error_test') IS NOT NULL DROP TABLE [dbo].[error_test]
GO
create table [dbo].[error_test] (a int NOT NULL , b int NOT NULL);
GO
insert into [error_test] (b) values (1);
GO
IF OBJECT_ID('dbo.test_error') IS NOT NULL DROP procedure dbo.test_error;
GO
CREATE PROCEDURE dbo.test_error (@e int OUTPUT)
AS
BEGIN
INSERT INTO [error_test] (b) values (1);
PRINT N'Error code in procedure = ' + CAST(@@error AS nvarchar(10));
SET @e = @@error;
END



                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                       Business Classification



                Legacy Error Handling- 2

DECLARE @ret int,
@e int;
EXEC @ret = dbo.test_error @e OUTPUT;
PRINT N'Returned error code = ' + CAST(@e AS nvarchar(10));
PRINT N'Return value = ' + CAST(@ret AS nvarchar(10));

Output :
Msg 515, Level 16, State 2, Procedure test_error, Line 4
Cannot insert the value NULL into column 'a', table 'tempdb.dbo.error_test';
column does not allow nulls. INSERT fails.
The statement has been terminated.
Error code in procedure = 515
Returned error code = 0
Return value = -6




                         Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                             Business Classification



                  Try...Catch Exception Handling
• Supported by SQL Server 2008
• Common in other modern programming languages.

•   The T-SQL statements in the BEGIN TRY...END TRY block execute normally.

•   If the block completes without error, the T-SQL statements between the BEGIN
    CATCH...END CATCH block are skipped.

•   If an exception is thrown by the statements in the TRY block, control transfers
    tothe statements in the BEGIN CATCH...END CATCH block.

•   CATCH Block Functions :
     –   ERROR_LINE() Returns the line number on which the exception occurred
     –   ERROR_MESSAGE() Returns the complete text of the generated error message
     –   ERROR_PROCEDURE() Returns the name of the SP or trigger where the error occurred
     –   ERROR_NUMBER() Returns the number of the error that occurred



                               Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



           Try...Catch Exception Handling-1


IF OBJECT_ID('dbo.test_error1') IS NOT NULL DROP procedure dbo.test_error1;
GO
CREATE PROCEDURE dbo.test_error1 (@e int OUTPUT)
AS
BEGIN
SET @e = 0;
BEGIN TRY
insert into [error_test] (b) values (1);
END TRY
BEGIN CATCH
SET @e = ERROR_NUMBER();
PRINT N'Error Code = ' + CAST(@e AS nvarchar(10));
PRINT N'Error Procedure = ' + ERROR_PROCEDURE();
PRINT N'Error Message = ' + ERROR_MESSAGE();
END CATCH
END
GO


                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



            Try...Catch Exception Handling-2
DECLARE @ret int,
@e int;
EXEC @ret = dbo.test_error1 @e OUTPUT;
PRINT N'Error code = ' + CAST(@e AS nvarchar(10));
PRINT N'Return value = ' + CAST(@ret AS nvarchar(10));

Output :

Error Code = 515
Error Procedure = test_error1
Error Message = Cannot insert the value
NULL into column 'a', table 'tempdb.dbo.error_test';
column does not allow nulls. INSERT fails.
Error code = 515
Return value = -6




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                               Business Classification



            THROW statement - SQL Server 2012
•   The THROW statement can be used to raise an exception and transfer execution
    to a CATCH block of a TRY…CATCH construct.

•   THROW command is different RAISERROR command :

     –   It is not mandatory to have a message available in sys.messages system object if you are using
         the error number or error ID.

     –    By default the exception thrown using the THROW command has a severity level of 16 and you
         cannot change it.

     –   You cannot use print style formatting with the THROW command although you can use the
         FORMATMESSAGE function to achieve the same results.

     –   When you re-throw the exception with the THROW keyword the original error number and line
         number is preserved unlike the RAISERROR command where it is overwritten.




                                 Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification



      THROW statement - SQL Server 2012 - 1

IF OBJECT_ID('dbo.test_error2') IS NOT NULL DROP procedure dbo.test_error2;
GO
CREATE PROCEDURE dbo.test_error2 (@e int OUTPUT)
AS
BEGIN
SET @e = 0;
BEGIN TRY
-- Force column does not allow nulls .
insert into [error_test] (b) values (1);
END TRY
BEGIN CATCH
PRINT 'In catch block.';
THROW;
END CATCH
END
GO




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                           Business Classification



        THROW statement - SQL Server 2012 - 2


DECLARE @ret int,
@e int;
EXEC @ret = dbo.test_error2 @e OUTPUT;

Output :
Msg 515, Level 16, State 2, Procedure test_error2, Line 6
Cannot insert the value NULL into column 'a', table 'tempdb.dbo.error_test';
column does not allow nulls. INSERT fails.




                             Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                    Business Classification


           SQL Server 2012 Sequence Numbers
CREATE SEQUENCE [schema_name . ] sequence_name
     [ <sequence_property_assignment> [ ,…n ] ]
  [;]
<sequence_property_assignment>::=
{
  [ AS { built_in_integer_type | user-defined_integer_type } ]
  | START WITH <constant>
     | INCREMENT BY <constant>
     | { MINVALUE <constant> | NO MINVALUE }
     | { MAXVALUE <constant> | NO MAXVALUE }
     | { CYCLE | NO CYCLE }
     | { CACHE [<constant> ] | NO CACHE }
}


                      Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


         SQL Server 2012 Sequence Numbers-
• CREATE SEQUENCE statement:
   – Specify the schema name
   – Specify the sequence name
   – Sequence type :
   – Any of the built-in integer types:
   – tinyint, smallint, int, bigint, decimal or numeric
   – decimal or numeric require a scale of 0
   – user-defined type that is based on one of the built-in integer
     types
   – Default sequence type is INT
   – Use START WITH <integer constant> to specify the first
     sequence number to be assigned; this can be a negative number
   – INCREMENT BY <integer constant> determines the next value
     assigned by the sequence number; this can be a positive or
     negative value but not 0
                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


         SQL Server 2012 Sequence Numbers-2
• CREATE SEQUENCE statement:
   – MINVALUE <integer constant> and MAXVALUE <integer
     constant> provide the bounds for the sequence number; the
     default for MINVALUE is 0 for a tinyint and the smallest negative
     number for the type of the sequence number; the default for
     MAXVALUE is the maximum value for the type of the sequence
     number
   – Specify CYCLE to restart the sequence number at the
     MINVALUE after the MAXVALUE is reached
   – Specify NO CYCLE to throw an exception after the MAXVALUE is
     reached rather than restarting with the MINVALUE
   – Use CACHE <integer constant> to retrieve a sequential block of
     sequence numbers
   – Use the NEXT VALUE FOR statement to assign and retrieve the
     next value for a sequence number.

                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                        Business Classification



         SQL Server 2012 Sequence Numbers-3
•   Caching
     – SQL server supports caching option for sequence (like Oracle)
     – Improved performance by minimizing disk access
     – NO CACHE to prevent caching
     – CACH for caching

•   Miscellaneous Notes
     – To retrieve a range of values from a sequence number, use the
       sys.sp_sequence_get_range stored procedure.
     – To retrieve information about sequence numbers, use the sys.sequences
       stored procedure
     – Other related Transact-SQL commands are ALTER SEQUENCE and DROP
       SEQUENCE.
     – The NEXT VALUE FOR statement is not run as part of the current
       transaction; a rollback does not affect the sequence number(s) already
       assigned.

                          Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


                      SEQUENCE example -1


USE tempdb ;
GO
IF OBJECT_ID('test.seq1','SO') IS NOT NULL
DROP SEQUENCE test.seq1;
GO
IF OBJECT_ID('test.RangeSeq','SO') IS NOT NULL
DROP SEQUENCE test.seq1;
GO
IF OBJECT_ID('test.seq1','SO') IS NOT NULL
DROP SEQUENCE test.seq3;
GO
IF OBJECT_ID('test.tbl_seq','T') IS NOT NULL
DROP TABLE test.tbl_seq;
GO
IF EXISTS   (select * from sys.schemas where name='test'                       )
BEGIN
drop schema test;
END ;


                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


                      SEQUENCE example -2

-- Create a schema named test
CREATE SCHEMA test;
GO

-- Create a sequence called dbo.Seq1:
CREATE SEQUENCE test.Seq1 AS INT START WITH 1 INCREMENT BY 1;


-- You invoke the following code in attempt to generate two sequence values:
SELECT NEXT VALUE FOR test.Seq1 AS val1, NEXT VALUE FOR test.Seq1 AS val2;

/*
This code doesn’t generate two different sequence values but rather just one, a
one value in both result columns val1 and val2:
val1        val2
----------- -----------
1           1
 All expressions that appear in the same logical phase (the SELECT in this case
conceptually evaluated at the same point in time
 */

                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification

*
                       SEQUENCE example -3
•  Retrieving a range of sequence values using sp_sequence_get_range
     • To request a new range of sequence values.
     •  Provide the sequence name and the range size as input.
         •  Output Parameters:
         •  The first value in the range
         •  The last value
         •  The number of times the sequence cycled (If allowed)
•   When asking for a new range exceeding the maximum (or minimum ,
if increment is negative)you will get error 11732.

/*
Example : The following statement gets four sequence numbers from the
Test.RangeSeq sequence object and returns the first of the numbers to the user.
*/
 CREATE SEQUENCE test.RangeSeq
    AS int
    START WITH 1
    INCREMENT BY 1
    MINVALUE 1
    MAXVALUE 25
    CYCLE
    CACHE 10;
                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification




                      SEQUENCE example -4

/* Get sequence metadata */
 SELECT current_value, is_cached,cache_size from sys.sequences
 WHERE OBJECT_ID=OBJECT_ID('test.RangeSeq')

current_value   is_cached              cache_size
-------------    ---------             ----------
1               1                      10

DECLARE @range_first_value sql_variant ,
        @range_first_value_output sql_variant ;

EXEC sp_sequence_get_range
@sequence_name = N'Test.RangeSeq'
, @range_size = 4
, @range_first_value = @range_first_value_output OUTPUT ;




                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                      Business Classification


*                       SEQUENCE example -5
Returning all output parameters
=================================
 The following example returns all the output values from the
sp_sequence_get_range procedure.
 */

DECLARE
  @FirstSeqNum sql_variant
, @LastSeqNum sql_variant
, @CycleCount int
, @SeqIncr sql_variant
, @SeqMinVal sql_variant
, @SeqMaxVal sql_variant ;

EXEC sys.sp_sequence_get_range
@sequence_name = N'Test.RangeSeq'
, @range_size = 5
, @range_first_value = @FirstSeqNum OUTPUT
, @range_last_value = @LastSeqNum OUTPUT
, @range_cycle_count = @CycleCount OUTPUT
, @sequence_increment = @SeqIncr OUTPUT
, @sequence_min_value = @SeqMinVal OUTPUT
, @sequence_max_value = @SeqMaxVal OUTPUT ;
                        Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification

                    SEQUENCE example -6
-- The following statement returns the output values
SELECT
  @FirstSeqNum AS FirstVal
, @LastSeqNum AS LastVal
, @CycleCount AS CycleCount
, @SeqIncr AS SeqIncrement
, @SeqMinVal AS MinSeq
, @SeqMaxVal AS MaxSeq ;

/*
output :
FirstVal LastVal CycleCount SeqIncrement MinSeq MaxSeq
5         9       0         1            1      25

FirtsVal is 5 because at first run @range_size = 4 .
LastVal is 9 because at second run @range_size = 5 so LastVal
is 4+5=9
*/


                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                  Business Classification


                   SEQUENCE example -7
/*
 Sequence Number Examples
The following examples will show the basic use cases for a
sequence number. The examples use the following schema and
table:
*/

USE [tempdb]
GO

CREATE TABLE test.tbl_seq (
  SeqNumber INT PRIMARY KEY
, Title VARCHAR(50)
)
GO




                    Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                   Business Classification


                  SEQUENCE example -8
/*
Create a sequence number in the test schema to assign tip
numbers on the test web site:
*/
CREATE SEQUENCE test.seq3
AS INT
START WITH 1
INCREMENT BY 1
MINVALUE 1
GO
/*
Get the next tip number and assign to a T-SQL variable (you
could use @TipNumber in the VALUES clause of one or more
INSERT statements):
*/

DECLARE @TipNumber As INT = NEXT VALUE FOR test.seq3
PRINT @TipNumber


                     Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                     Business Classification


                    SEQUENCE example -9
/*
Insert a row into a table and get the next value for the
TipNumber:
*/


INSERT INTO test.tbl_seq (SeqNumber, Title)
VALUES (NEXT VALUE FOR test.seq3, 'SQL 2012 - Sequence Numbers
DEMO');
select * from test.tbl_seq;

DROP   SEQUENCE test.seq1;
DROP   SEQUENCE test.seq3;
DROP    SEQUENCE test.RangeSeq;
DROP   TABLE test.tbl_seq;
drop   schema test ;




                       Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                               Business Classification



           SQL Server 2012 Sequence Numbers-4
•   A column in a table can have the IDENTITY property which provides for
    automatically assigning the next value to the column on insert.
•   Compare sequence number with the identity column.

     –    An identity column is tied to a table; you have to insert a row in order to get the next value.
     –   You can use the NEXT VALUE FOR statement to assign and retrieve the next value for a
         sequence number; it is not tied to a particular column in a table and you do not have to
         insert a row to get the next value.

     –   A sequence number can be used to provide an automatically generated number that is used
         over multiple tables; the identity column is tied to a single table.

     –   identity column : Specify the starting value and increment with an ;
     –   sequence number : you can specify the starting value, increment, minimum, maximum,
         caching, and whether to recycle the values

     –   The NEXT VALUE FOR statement includes an OVER clause allowing you to assign sequence
         numbers based on an ORDER BY; an identity column does not have this capability.




                                 Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                                                Business Classification



           SQL Server 2012 Sequence Numbers-5
•   Choose a sequence number in these scenarios:
     –   You want the next value without having to insert a row into a table.
     –   You want to use the value across multiple tables.
     –   You want to automatically recycle the values.


•   Choose an IDENTITY in these scenarios:
     –   You want to assign the next value on insert for a single table.




•   Currently , the size of IDENTITY is a hard coded value of 10.
•   the default cache size is 50 for sequence .
•   If the system shuts unexpectedly , you lose the remaining cached values and end
    up with a gap ..
•   with IDENTITY , during recovery it scans the log to find out last used IDENTITY ,
    so you end up not losing the remaining cached values



                                  Proprietary of Rafael - Advanced Defense Systems.Ltd.
Security Classification
                 Business Classification


Discussion, questions &
       answers




   Proprietary of Rafael - Advanced Defense Systems.Ltd.

Weitere ähnliche Inhalte

Was ist angesagt?

Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Supportnkarag
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding GuidelinesChris Adkin
 
Oracle Course
Oracle CourseOracle Course
Oracle Courserspaike
 
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1Navneet Upneja
 
Optimizer overviewoow2014
Optimizer overviewoow2014Optimizer overviewoow2014
Optimizer overviewoow2014Mysql User Camp
 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012Eduardo Castro
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
Oracle Data Redaction - EOUC
Oracle Data Redaction - EOUCOracle Data Redaction - EOUC
Oracle Data Redaction - EOUCAlex Zaballa
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Jaime Crespo
 

Was ist angesagt? (9)

Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Oracle Course
Oracle CourseOracle Course
Oracle Course
 
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1
 
Optimizer overviewoow2014
Optimizer overviewoow2014Optimizer overviewoow2014
Optimizer overviewoow2014
 
TSQL in SQL Server 2012
TSQL in SQL Server 2012TSQL in SQL Server 2012
TSQL in SQL Server 2012
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Oracle Data Redaction - EOUC
Oracle Data Redaction - EOUCOracle Data Redaction - EOUC
Oracle Data Redaction - EOUC
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
 

Ähnlich wie Fast transition to sql server 2012 from mssql 2005 2008 for developers - David Yitzhak

What's New in IBM Java 8 SE?
What's New in IBM Java 8 SE?What's New in IBM Java 8 SE?
What's New in IBM Java 8 SE?Tim Ellison
 
Bhabani_Bal -RMS_DWH
Bhabani_Bal -RMS_DWHBhabani_Bal -RMS_DWH
Bhabani_Bal -RMS_DWHBhabani Bal
 
Java in the database–is it really useful? Solving impossible Big Data challenges
Java in the database–is it really useful? Solving impossible Big Data challengesJava in the database–is it really useful? Solving impossible Big Data challenges
Java in the database–is it really useful? Solving impossible Big Data challengesRogue Wave Software
 
Oracle Erp solutions
Oracle Erp solutionsOracle Erp solutions
Oracle Erp solutionsNamit Sahai
 
Rapid Tools(swift_suite) intro
Rapid Tools(swift_suite) introRapid Tools(swift_suite) intro
Rapid Tools(swift_suite) introJulie Jackson
 
2. buc od-vl-sparc-today tomorrow-v1.5
2. buc od-vl-sparc-today tomorrow-v1.52. buc od-vl-sparc-today tomorrow-v1.5
2. buc od-vl-sparc-today tomorrow-v1.5Doina Draganescu
 
Mainframes and Cobol Migration - Tools based - Arkin Software
Mainframes and Cobol Migration - Tools based - Arkin SoftwareMainframes and Cobol Migration - Tools based - Arkin Software
Mainframes and Cobol Migration - Tools based - Arkin Softwarearkinsoftware
 
Resume_Mohammed_Ali_Oracle
Resume_Mohammed_Ali_OracleResume_Mohammed_Ali_Oracle
Resume_Mohammed_Ali_OracleMohammed Ali
 
Practical SAP pentesting workshop (NullCon Goa)
Practical SAP pentesting workshop (NullCon Goa)Practical SAP pentesting workshop (NullCon Goa)
Practical SAP pentesting workshop (NullCon Goa)ERPScan
 
From Nice to Have to Mission Critical: MySQL Enterprise Edition
From Nice to Have to Mission Critical: MySQL Enterprise EditionFrom Nice to Have to Mission Critical: MySQL Enterprise Edition
From Nice to Have to Mission Critical: MySQL Enterprise Edition郁萍 王
 
C19013010 the tutorial to build shared ai services session 2
C19013010 the tutorial to build shared ai services session 2C19013010 the tutorial to build shared ai services session 2
C19013010 the tutorial to build shared ai services session 2Bill Liu
 
Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...
Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...
Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...Hirofumi Iwasaki
 
Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...
Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...
Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...avanttic Consultoría Tecnológica
 
Oracle apps dba Training in Chennai
Oracle apps dba Training in ChennaiOracle apps dba Training in Chennai
Oracle apps dba Training in Chennaikomalpreethi
 
Oracle apps dba Training in Chennai
Oracle apps dba Training in ChennaiOracle apps dba Training in Chennai
Oracle apps dba Training in Chennaikomalpreethi
 

Ähnlich wie Fast transition to sql server 2012 from mssql 2005 2008 for developers - David Yitzhak (20)

What's New in IBM Java 8 SE?
What's New in IBM Java 8 SE?What's New in IBM Java 8 SE?
What's New in IBM Java 8 SE?
 
Bhabani_Bal -RMS_DWH
Bhabani_Bal -RMS_DWHBhabani_Bal -RMS_DWH
Bhabani_Bal -RMS_DWH
 
Java in the database–is it really useful? Solving impossible Big Data challenges
Java in the database–is it really useful? Solving impossible Big Data challengesJava in the database–is it really useful? Solving impossible Big Data challenges
Java in the database–is it really useful? Solving impossible Big Data challenges
 
Oracle Erp solutions
Oracle Erp solutionsOracle Erp solutions
Oracle Erp solutions
 
Rapid Tools(swift_suite) intro
Rapid Tools(swift_suite) introRapid Tools(swift_suite) intro
Rapid Tools(swift_suite) intro
 
2. buc od-vl-sparc-today tomorrow-v1.5
2. buc od-vl-sparc-today tomorrow-v1.52. buc od-vl-sparc-today tomorrow-v1.5
2. buc od-vl-sparc-today tomorrow-v1.5
 
Tips and Tricks for Toad
Tips and Tricks for ToadTips and Tricks for Toad
Tips and Tricks for Toad
 
Mainframes and Cobol Migration - Tools based - Arkin Software
Mainframes and Cobol Migration - Tools based - Arkin SoftwareMainframes and Cobol Migration - Tools based - Arkin Software
Mainframes and Cobol Migration - Tools based - Arkin Software
 
Resume_Mohammed_Ali_Oracle
Resume_Mohammed_Ali_OracleResume_Mohammed_Ali_Oracle
Resume_Mohammed_Ali_Oracle
 
Practical SAP pentesting workshop (NullCon Goa)
Practical SAP pentesting workshop (NullCon Goa)Practical SAP pentesting workshop (NullCon Goa)
Practical SAP pentesting workshop (NullCon Goa)
 
From Nice to Have to Mission Critical: MySQL Enterprise Edition
From Nice to Have to Mission Critical: MySQL Enterprise EditionFrom Nice to Have to Mission Critical: MySQL Enterprise Edition
From Nice to Have to Mission Critical: MySQL Enterprise Edition
 
C19013010 the tutorial to build shared ai services session 2
C19013010 the tutorial to build shared ai services session 2C19013010 the tutorial to build shared ai services session 2
C19013010 the tutorial to build shared ai services session 2
 
Upgrading Em13c Collaborate 2016
Upgrading Em13c Collaborate 2016Upgrading Em13c Collaborate 2016
Upgrading Em13c Collaborate 2016
 
AhmedReda
AhmedRedaAhmedReda
AhmedReda
 
Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...
Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...
Java EE 7 with Apache Spark for the World’s Largest Credit Card Core Systems ...
 
Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...
Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...
Meetup Oracle Database: 3 Analizar, Aconsejar, Automatizar… las nuevas funcio...
 
Ayman_Resume - update
Ayman_Resume - updateAyman_Resume - update
Ayman_Resume - update
 
I one Service Offerings
I one Service OfferingsI one Service Offerings
I one Service Offerings
 
Oracle apps dba Training in Chennai
Oracle apps dba Training in ChennaiOracle apps dba Training in Chennai
Oracle apps dba Training in Chennai
 
Oracle apps dba Training in Chennai
Oracle apps dba Training in ChennaiOracle apps dba Training in Chennai
Oracle apps dba Training in Chennai
 

Mehr von sqlserver.co.il

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013sqlserver.co.il
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cachesqlserver.co.il
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013sqlserver.co.il
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserversqlserver.co.il
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012sqlserver.co.il
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum versionsqlserver.co.il
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3sqlserver.co.il
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2sqlserver.co.il
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1sqlserver.co.il
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Eventssqlserver.co.il
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoresqlserver.co.il
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACsqlserver.co.il
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatialsqlserver.co.il
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkelsqlserver.co.il
 

Mehr von sqlserver.co.il (20)

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cache
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013
 
DAC 2012
DAC 2012DAC 2012
DAC 2012
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserver
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum version
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStore
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DAC
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatial
 
מיכאל
מיכאלמיכאל
מיכאל
 
נועם
נועםנועם
נועם
 
עדי
עדיעדי
עדי
 
מיכאל
מיכאלמיכאל
מיכאל
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 

Kürzlich hochgeladen

How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 

Kürzlich hochgeladen (20)

How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 

Fast transition to sql server 2012 from mssql 2005 2008 for developers - David Yitzhak

  • 1. Security Classification Business Classification Fast Transition To SQL 2012 from MSSQL 2005/2008 For Developers – cimid@rafael.co.il Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 2. Security Classification Business Classification Rafael Advanced Defense Systems • Designs, develops, manufactures and supplies a wide range of high tech defense systems for air, land, sea and space applications. • Sales in 2010 exceeding $1851M • About 7000 Employees Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 3. Security Classification Business Classification Speaker Qualifications • SQL Server , Oracle , Sybase ,Sybase Anywhere - application & infrastructure DBA . • 16 years of SQL experience, starting with version 6.5 • Education : – BSC (Information System Engineer) – MBA – Currently ME (System Engineering) Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 4. Security Classification Business Classification Previous Lectures-OOW 2010 RAFDOCS-#3545730-ORACLE -ORACLE OPEN WORLD 2010 - Rafael Mission-Critical Use of Oracle Active Data Guard/Oracle .NET Technology Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 5. Security Classification Business Classification Previous Lectures-1 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 6. Security Classification Business Classification Previous Lectures-2 Database Mirror for the exceptional DBA – cimid@rafael.co.il Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 7. Security Classification Business Classification Previous Lectures-3 When ODAC ,Oracle Sever 11GR2, Win HPC Server 2008 Come together… David Itshak Rafael Advanced Defense System cimid@rafael.co.il Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 8. Security Classification Business Classification Previous Lectures-4 DRP shaked19@gmail.com Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 9. Security Classification Business Classification Previous Lectures-5 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 10. Security Classification Business Classification Agenda • Using the old & new Analytic Functions in SQL 2012 – SQL Server 2005 and 2008 Ranking Functions Row_Number and Rank – Ranking functions – Inverse percentile functions – Window functions – Reporting functions – Lag and lead functions – Using the LAG and LEAD Functions – First and last functions • OVER Clause Support Enhanced Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 11. Security Classification Business Classification Agenda • 14 New Functions and 1 Changed Function – Type Conversion function : From cast & convert to : • PARSE • TRY_CONVERT • TRY_PARSE – New Logical Functions : IIF and CHOOSE. – New Date and Time Functions. – String functions : CONCAT & FORMAT • Overview of WITH RESULT SETS Feature Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 12. Security Classification Business Classification Agenda • From RAISERROR to THROW statement to raise an exception • Moving From Identity Column to Sequence Number . • What Next I Expect in Next Version of SQL Server under the slides with “In Next version maybe” Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 13. Security Classification Business Classification Next Lecture • Ad-hoc Query Paging Implementation – The old way stored procedures to page large tables or queries – OFFST/FTECH Filter in SQL 2012 • New DMVs to get configuration, installation and memory dump information Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 14. Security Classification Business Classification Next Lecture • Contained Database - The end of Orphan users . • From FileStream to FileTables • Power shell : From Backup with SMO to Restore- SqlDatabase cmdlet • SQL 2012 Spatial Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 15. Security Classification Business Classification Exercise material Based on original Material !!! Programmability Enhancements (Database Engine) SQL 2012 , Demo script Script Name : fast_transition_to_mssql_2012.sql Author:Dadid Itshak Email:cimid@rafael.co.il Date:Feb 6th 2012 Content : Modification History: Date Who What Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 16. Security Classification Business Classification NEW Analytic Functions • The following analytic functions have been added to SQL Server 2012. – CUME_DIST – LAST_VALUE – PERCENTILE_DISC – FIRST_VALUE – LEAD – PERCENT_RANK – LAG Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 17. Security Classification Business Classification OVER Clause Support Enhanced • The OVER clause has been extended to support window functions. • Window functions perform a calculation across a set of rows that are in some relationship to the current row. – use the ROWS or RANGE clause over a set of rows to calculate a moving average or cumulative total.) • Ordering rows within a partition is now supported in the aggregate functions that allow the OVER clause to be specified. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 18. Security Classification Business Classification NEW Analytic Functions and OVER Clause Support Enhanced Source : Window Functions in SQL Server: Part 2-The Frame ,24 November 2011 by Fabiano Amori , Simple-Talk.com Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 19. Security Classification Business Classification Using the Analytic Functions • DB has many built-in analytic functions that enable you to perform complex calculations, such as finding the top-selling product type for each month, the top salespersons, and so on. • Analytic functions are organized into the following categories: – Ranking functions enable you to calculate ranks, percentiles, and n- tiles (tertiles, quartiles, and so on). – Window functions enable you to calculate cumulative and moving aggregates. – Lag and lead functions enable you to get a value in a row where that row is a certain number of rows away from the current row. – First and last functions enable you to get the first and last values in an ordered group. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 20. Security Classification Business Classification Using the Analytic Functions 1 • In Next version maybe : – Inverse percentile functions enable you to calculate the value that corresponds to a percentile. – Hypothetical rank and distribution functions enable you to calculate the rank and – percentile that a new row would have if you inserted it into a table. – Linear regression functions enable you to fit an ordinary- least-squares regression line to a set of number pairs. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 21. Security Classification Business Classification Analytic Functions Description USE TempDB GO IF OBJECT_ID('Tab1') IS NOT NULL DROP TABLE Tab1 GO CREATE TABLE Tab1 (Col1 INT) GO INSERT INTO Tab1 VALUES(5), (5), (3) , (1) GO Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 22. Security Classification Business Classification Analytic Functions Description - 1 • RANK() : – Returns the rank of items in a group. – leaves a gap in the sequence of rankings in the event of a tie. • DENSE_RANK(): – Returns the rank of items in a group. – Doesn’t leave a gap in the sequence of rankings in the event of a tie. SELECT Col1, RANK() OVER(ORDER BY Col1 DESC) AS "RANK()" FROM Tab1 GO SELECT Col1, DENSE_RANK() OVER(ORDER BY Col1 DESC) AS "DENSE_RANK" FROM Tab1 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 23. Security Classification Business Classification Analytic Functions Description - 2 • CUME_DIST() : – Short for cumulative distribution. – Returns the position of a specified value relative to a group of values. If X is a purely discrete random variable, then it attains values x1, x2, ... with probability pi = P(xi) Cumulative distribution function of a discrete probability distribution Properties : Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 24. Security Classification Business Classification Analytic Functions Description - 3 • PERCENT_RANK(): – Returns the percent rank of a value relative to a group of values. • NTILE() Returns n-tiles: tertiles, quartiles, and so on. SELECT Col1, NTILE(3) OVER(ORDER BY Col1 DESC) AS "NTILE(3)" FROM Tab1 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 25. Security Classification Business Classification Analytic Functions Description - 4 • ROW_NUMBER() Returns a number with each row in a group SELECT Col1, ROW_NUMBER() OVER(ORDER BY Col1 DESC) AS "ROW_NUMBER()" FROM Tab1 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 26. Security Classification Business Classification Using the Analytic Functions -Example Table /* The Example Table The total_sales table stores the sum of all the sales by dollar amount for a particular year, month, product type, and employee. */ CREATE TABLE total_sales ( year INTEGER NOT NULL, month INTEGER NOT NULL, prd_type_id INTEGER, emp_id INTEGER, amount NUMERIC, CONSTRAINT total_sales_pk PRIMARY KEY ( year, month, prd_type_id, emp_id ); Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 27. Security Classification Business Classification Using the Analytic Functions -Example Table /* total_sales table contains five columns, as follows: YEAR stores the year the sales took place. MONTH stores the month the sales took place (1 to 12). PRD_TYPE_ID stores the product_type_id of the product. EMP_ID stores the employee_id of the employee who handled the sales. AMOUNT stores the total dollar amount of the sales. The following query retrieves the first 12 rows from the total_sales table: */ select top (12) * from [dbo].[total_sales] Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 28. Security Classification Business Classification Using the RANK() and DENSE_RANK() Functions • You use RANK() and DENSE_RANK() to rank items in a group. • The difference : – RANK() leaves a gap in the sequence when there is a tie – DENSE_RANK() leaves no gaps. • Example . if you were ranking sales by product type and two product types tie for first place – RANK() would put the two product types in first place, but the next product type would be in third place. – DENSE_RANK() would also put the wo product types in first place, but the next product type would be in second place. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 29. Security Classification Business Classification Using the RANK() and DENSE_RANK() Functions -1 SELECT prd_type_id, SUM(amount), RANK() OVER (ORDER BY SUM(amount) DESC) AS rank, DENSE_RANK() OVER (ORDER BY SUM(amount) DESC) AS dense_rank FROM total_sales WHERE year = 2012 AND amount IS NOT NULL GROUP BY prd_type_id ORDER BY prd_type_id; • Notice : – No ties => RANK() and DENSE_RANK() return the same ranks. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 30. Security Classification Business Classification Controlling Ranking of Null Values - Next version maybe • Using the NULLS FIRST and NULLS LAST Clauses When using an analytic function • Explicitly control whether nulls are the highest or lowest in a group . • Example : Use NULLS LAST to specify that nulls are the lowest: SELECT prd_type_id, SUM(amount), RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST) AS rank, DENSE_RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST) AS dense_rank FROM total_sales WHERE year = 2012 GROUP BY prd_type_id ORDER BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 31. Security Classification Business Classification Using the PARTITION BY Clause with Analytic Functions • When you need to divide the groups into subgroups. • Example : Subdivide the sales amount by month, use PARTITION BY month as follows : SELECT prd_type_id, month, SUM(amount) AS SUM_Amount , RANK() OVER (PARTITION BY month ORDER BY SUM(amount) DESC) AS rank FROM total_sales WHERE year = 2012 AND amount IS NOT NULL GROUP BY prd_type_id, month ORDER BY prd_type_id, month; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 32. Security Classification Business Classification Using ROLLUP with Analytic Function • Use ROLLUP and RANK() to get the sales rankings by product type ID: SELECT prd_type_id, SUM(amount) SUM_Amount, RANK() OVER (ORDER BY SUM(amount) DESC) AS rank FROM total_sales WHERE year = 2012 GROUP BY ROLLUP(prd_type_id) ORDER BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 33. Security Classification Business Classification Using CUBE and RANK() with Analytic Function • Use CUBE and RANK() to get all rankings of sales by product type ID an employee ID : SELECT prd_type_id, emp_id, SUM(amount)as SUM_Amount, RANK() OVER (ORDER BY SUM(amount) DESC) AS rank FROM total_sales WHERE year = 2012 GROUP BY CUBE(prd_type_id, emp_id) ORDER BY prd_type_id, emp_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 34. Security Classification Business Classification Using GROUPING SETS and RANK() with Analytic Function • Uses GROUPING SETS and RANK() to get just the sales amount subtotal • GROUPING SETS gets just the subtotal rows. In following example subtotals for product type id and employee id . SELECT prd_type_id, emp_id, SUM(amount) SUM_Amount, RANK() OVER (ORDER BY SUM(amount) DESC) AS rank FROM total_sales WHERE year = 2012 GROUP BY GROUPING SETS(prd_type_id, emp_id) ORDER BY prd_type_id, emp_id Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 35. Security Classification Business Classification Using the CUME_DIST() and PERCENT_RANK() Functions • CUME_DIST() to calculate the position of a specified value relative to a group of values. • CUME_DIST() is short for cumulative distribution. • PERCENT_RANK() to calculate the percent rank of a value relative to a group of values. • Example : Use of CUME_DIST() and PERCENT_RANK() to get the cumulative distribution and percent rank of sales: SELECT prd_type_id, SUM(amount) SUM_Amount, CUME_DIST() OVER (ORDER BY SUM(amount) ASC) AS cume_dist, PERCENT_RANK() OVER (ORDER BY SUM(amount) ASC) AS percent_rank FROM total_sales WHERE year = 2012 GROUP BY prd_type_id ORDER BY prd_type_id Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 36. Security Classification Business Classification Using the CUME_DIST() and PERCENT_RANK() Functions - SELECT prd_type_id, SUM(amount) SUM_Amount, CUME_DIST() OVER (ORDER BY SUM(amount) ASC) AS cume_dist, PERCENT_RANK() OVER (ORDER BY SUM(amount) ASC) AS percent_rank FROM total_sales WHERE year = 2012 GROUP BY prd_type_id ORDER BY prd_type_id Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 37. Security Classification Business Classification Using the NTILE() Function • You use NTILE(buckets) to calculate n-tiles (tertiles, quartiles, and so on); – buckets specifies the number of “buckets” into which groups of rows are placed. • Example : 4 is passed to NTILE() to split the groups of rows into four buckets SELECT prd_type_id, SUM(amount) SUM_Amount, NTILE(4) OVER (ORDER BY SUM(amount) DESC) AS ntile FROM total_sales WHERE year = 2012 AND amount IS NOT NULL GROUP BY prd_type_id ORDER BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 38. Security Classification Business Classification Using the ROW_NUMBER() Function • You use ROW_NUMBER() to return a number with each row in a group, starting at 1. Example : SELECT prd_type_id, SUM(amount) SUM_Amount, ROW_NUMBER() OVER (ORDER BY SUM(amount) DESC) AS row_number FROM total_sales WHERE year = 2012 GROUP BY prd_type_id ORDER BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 39. Security Classification Business Classification Use the inverse percentile functions -Next version maybe • 2 inverse percentile functions: PERCENTILE_DISC(x)and PERCENTILE_CONT(x). • Reverse of CUME_DIST() and PERCENT_RANK(). • PERCENTILE_DISC(x) examines the cumulative distribution values in each group until it finds one that is greater than or equal to x. • PERCENTILE_CONT(x) examines the percent rank values in each group until it finds one that is greater than or equal to x. • Example how to get the sum of the amount whose percentile is greater than or equal to 0.6 SELECT PERCENTILE_CONT(0.6) WITHIN GROUP (ORDER BY SUM(amount) DESC) AS percentile_cont, PERCENTILE_DISC(0.6) WITHIN GROUP (ORDER BY SUM(amount) DESC) AS percentile_disc FROM total_sales WHERE year = 2012 GROUP BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 40. Security Classification Business Classification Using the Window Function • Calculate things like cumulative sums and moving averages within a specified range of rows, a range of values, or an interval of time. • “window” describes a subset of rows within the result set. • The subset of rows in window is processed by the window functions, which return a value. • You can define the start and end of the window. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 41. Security Classification Business Classification Using the Window Function-WINDOWS FRAME • OVER ( [ <PARTITION BY clause> ] [ <ORDER BY clause> ] [ <ROW or RANGE clause> ] ) • “window” describes a subset of rows within the result set. • The subset of rows in window is processed by the window functions, which return a value. • You can define the start and end of the window. • windows correspond to each distinct CustomerID : Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 42. Security Classification Business Classification Using the Window Function-WINDOWS FRAME [ROWS | RANGE] BETWEEN <Start expr> AND <End expr> Where: <Start expr> is one of: – UNBOUNDED PRECEDING: The window starts in the first row of the partition – CURRENT ROW: The window starts in the current row – <unsigned integer literal> PRECEDING or FOLLOWING <End expr> is one of: – UNBOUNDED FOLLOWING: The window ends in the last row of the partition – CURRENT ROW: The window ends in the current row – <unsigned integer literal> PRECEDING or FOLLOWING • Where it is not explicitly specified, the default window frame is “range between unbounded preceding and current row”, – the top row in the window is the first row in the current partition, and the bottom row in the window is the current row. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 43. Security Classification Business Classification Performing a Cumulative Sum • Example : a cumulative sum to compute the cumulative sales amount for 2012, from January to December; • Notice : each monthly sales amount is added to the cumulative amount that grows after each month: SELECT month, SUM(amount) AS month_amount, SUM(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_amount FROM total_sales WHERE year = 2012 GROUP BY month ORDER BY month; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 44. Security Classification Business Classification Performing a Cumulative Sum- How It Works ? - 1 • Computes the cumulative total of the sales amounts, starting at month 1, then month1 + month 2, then month1 + month 2 + month 3, and so on, up to and including month 12. SUM(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_amount • SUM(amount) computes the sum of an amount. • The outer SUM() computes the cumulative amount. • ORDER BY month orders the rows read by the query by month. • ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW defines the start and end of the window. • The start is set to UNBOUNDED PRECEDING- the start of the window is fixed at the first row in the result set returned by the query. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 45. Security Classification Business Classification Performing a Cumulative Sum- How It Works ? - 2 • The end of the window is set to CURRENT ROW; • CURRENT ROW- represents the current row in the result set being processed, and the end of the window slides down one row after the outer SUM() function computes and returns the current cumulative amount. • The start of the window is fixed at month 1, but the bottom of the window moves down one row in the result set after each month’s sales amounts are added to the cumulative total. • This continues until the last row in the result set is processed by the window and the SUM() functions. – (i.e., the sum of the sales amount for that month is added to the cumulative total). Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 46. Security Classification Business Classification Performing a Cumulative Sum- How It Works ? - 3 • Don’t confuse the end of the window with the end of the result set. In the previous example, the end of the window slides down one row in the result set as each row is processed – (i.e., the sum of the sales amount for that month is added to the cumulative total). • In the example, the end of the window starts at the first row, the sum sales amount for that month is added to the cumulative total, and then the end of the window moves down one row to the second row. At this point, the window sees two rows. • The sum of the sales amount for that month is added to the cumulative total, and the end of the window moves down one row to the third row. At this point, the window sees three rows. This continues until the twelfth row is processed. At this point, the window sees twelve rows. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 47. Security Classification Business Classification Performing a Cumulative Sum - CONT • Example uses a cumulative sum to compute the cumulative sales amount, start with June of 2012 (month 6) and ending in December of 2012 (month 12 SELECT month, SUM(amount) AS month_amount, SUM(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_amount FROM total_sales WHERE year = 2012 AND month BETWEEN 6 AND 12 GROUP BY month ORDER BY month; Ex : month 1 cumulative_amount =(160221.98+2972073.67)=1132295.6 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 48. Security Classification Business Classification Performing a Moving Average • Example : Moving average of the sales amount between the current month and the previous 3 months: SELECT month, SUM(amount) AS month_amount, AVG(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) AS moving_average FROM total_sales WHERE year = 2012 GROUP BY month ORDER BY month; Example : month 11 moving_average= 209262.032500= (160221.98+264480.79+199609.68+212735.68)/4 month 2 moving_average= 106098.575000= (116671.60+95525.55)/2 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 49. Security Classification Business Classification Performing a Moving Average- How It Works ? - 1 • The query uses the following expression to compute the moving average: AVG(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) AS moving_average • SUM(amount) computes the sum of an amount. • The outer AVG() computes the average • ORDER BY month orders the rows read by the query by month. • ROWS BETWEEN 3 PRECEDING AND CURRENT ROW : – Defines the start of the window as including the three rows preceding the current row; – The end of the window is the current row being processed. – Entire expression computes the moving average of the sales amount between the – current month and the previous three months. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 50. Security Classification Business Classification Performing a Moving Average- How It Works ? - 2 • For first 2 months less than 3 months , moving average is based on only the months available. • Both the start and the end of the window begin at row #1 read by the query. • The end of the window moves down after each row is processed. • The start of the window moves down only after row no 4 has been processed, and subsequently moves down one row after each row is processed . Until the last row in the result . Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 51. Security Classification Business Classification Performing a Centered Average • Example : Computes the moving average of the sales amount centered between the previous and next month from the current month: SELECT month, SUM(amount) AS month_amount, AVG(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS moving_average FROM total_sales WHERE year = 2012 GROUP BY month ORDER BY month; Example : month 5 moving average=(175998.80+154349.44+124951.36)/3=151766.533333 month 1 moving average=(95525.55+116671.60)/2 =106098.575000 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 52. Security Classification Business Classification Performing a Centered Average-How It Works ? - 1 AVG(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS moving_average • SUM(amount) computes the sum of an amount. • The outer AVG() computes the average. • ORDER BY month orders the rows read by the query by month. • ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING defines the start of the window as including the row preceding the current row being processed. • The end of the window is the row following the current row. • The entire expression computes the moving average of the sales amount between the current month and the previous month. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 53. Security Classification Business Classification Performing a Centered Average-How It Works ? - 2 • Because for the first and last month less than the full three months of data are available, the moving average is based on only the months available. • The start of the window begins at row No 1 read by the query. • The end of the window begins at row No 2 and moves down after each row is processed. • The start of the window moves down only once row #2 has been processed. • Processing continues until the last row read by the query is processed. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 54. Security Classification Business Classification Getting the First and Last Rows Using FIRST_VALUE() and LAST_VALUE() • Use the FIRST_VALUE() and LAST_VALUE() functions to get the first and last rows in a window. • Example : use FIRST_VALUE() and LAST_VALUE() to get the previous and next month’s sales amount SELECT month, SUM(amount) AS month_amount, FIRST_VALUE(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS previous_month_amount, LAST_VALUE(SUM(amount)) OVER (ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS next_month_amount FROM total_sales WHERE year = 2012 GROUP BY month ORDER BY month; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 55. Security Classification Business Classification Using the Reporting Function • You use the reporting functions to perform calculations across groups and partitions within groups. • You can perform reporting with the following functions: SUM(), AVG(), MAX(), MIN(),COUNT(), VARIANCE(), and STDDEV(). • For the first three months of 2012, the following query reports • Total sum of all sales for all three months (total_month_amount). • Total sum of all sales for all product types ( total_product_type_amount). SELECT month, prd_type_id, SUM(SUM(amount)) OVER (PARTITION BY month) AS total_month_amount, SUM(SUM(amount)) OVER (PARTITION BY prd_type_id) AS total_product_type_amount FROM total_sales WHERE year = 2012 AND month <= 3 GROUP BY month, prd_type_id ORDER BY month, prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 56. Security Classification Business Classification Using the LAG() and LEAD() Functions • You use the LAG() and LEAD() functions to get a value in a row where that row is a certain number of rows away from the current row. • The following query uses LAG() and LEAD() to get the previous and next month’s sales amount: SELECT month, SUM(amount) AS month_amount, LAG(SUM(amount), 1) OVER (ORDER BY month) AS previous_month_amount, LEAD(SUM(amount), 1) OVER (ORDER BY month) AS next_month_amount FROM total_sales WHERE year = 2012 GROUP BY month ORDER BY month; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 57. Security Classification Business Classification Using the LAG() and LEAD() Functions- It Works ? • LAG(SUM(amount), 1) gets the previous row’s sum of the amount. • LEAD(SUM(amount),1) gets the next row’s sum of the amount. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 58. Security Classification Business Classification Using the Hypothetical Rank and Distribution Functions- Next version maybe ? • Use the hypothetical rank and distribution functions to calculate the rank and percentile that a new row would have if you inserted it into a table. • Perform hypothetical calculations with the following functions: RANK(), DENSE_RANK(), PERCENT_RANK(), and CUME_DIST() to get the rank and percent rank of sales by product type for 2012: SELECT prd_type_id, SUM(amount), RANK() OVER (ORDER BY SUM(amount) DESC) AS rank, PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank FROM all_sales WHERE year = 2003 AND amount IS NOT NULL GROUP BY prd_type_id ORDER BY prd_type_id Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 59. Security Classification Business Classification Using the Hypothetical Rank and Distribution Functions- Next version maybe ? • Use the hypothetical rank and distribution functions to calculate the rank and percentile that a new row would have if you inserted it into a table. • Perform hypothetical calculations with the following functions: RANK(), DENSE_RANK(), PERCENT_RANK(), and CUME_DIST() to get the rank and percent rank of sales by product type for 2012: SELECT prd_type_id, SUM(amount), RANK() OVER (ORDER BY SUM(amount) DESC) AS rank, PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank FROM all_sales WHERE year = 2003 AND amount IS NOT NULL GROUP BY prd_type_id ORDER BY prd_type_id Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 60. Security Classification Business Classification Using the Hypothetical Rank and Distribution Functions- Next version maybe ? 1 • The next query shows the hypothetical rank and percent rank of a sales amount of $500,000: SELECT RANK(500000) WITHIN GROUP (ORDER BY SUM(amount) DESC) AS rank, PERCENT_RANK(500000) WITHIN GROUP (ORDER BY SUM(amount) DESC) AS percent_rank FROM all_sales RANK PERCENT_RANK WHERE year = 2003 0.25 AND amount IS NOT NULL GROUP BY prd_type_id ORDER BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 61. Security Classification Business Classification 14 New Functions and 1 Changed Function • New Type Conversion function : – PARSE – TRY_CONVERT – TRY_PARSE • New Logical Functions : IIF and CHOOSE. • New Date and Time Functions. • String functions : CONCAT & FORMAT Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 62. Security Classification Business Classification PARSE Conversion Function • The PARSE function converts any string value to a Numeric or to a Date and Time format. • For general type conversions, continue to use CAST or CONVERT. • Performance overhead in parsing the string value. • PARSE relies on the presence of .the .NET Framework Common Language Runtime (CLR). • Supports an optional USING clause indicating the culture. • The culture is any valid culture supported by the .NET framework. • If not specified ,uses the current session’s effective language Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 63. Security Classification Business Classification PARSE Conversion Function- 1 /* Using PARSE Function to Convert String to Date Time */ SELECT PARSE('2/06/2012' AS datetime) AS [Using PARSE Function] GO Output: Using PARSE Function 2012-02-06 00:00:00.000 /* PARSE with implicit setting of language The English language is mapped to en-US specific culture */ SET LANGUAGE 'English' SELECT PARSE('02/06/2012' AS datetime2) AS Result Output: Result 2012-02-06 00:00:00.0000000 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 64. Security Classification Business Classification PARSE Conversion Function- 2 /* Using PARSE Function to Convert String to INT */ SELECT TRY_PARSE('ONE' AS INT) AS [Using TRY_PARSE Function] Output : Using TRY_PARSE Function NULL /* Using PARSE Function to Convert String to Date Time */ SELECT TRY_PARSE('02/06/2012' AS datetime) AS [Using TRY_PARSE Function] GO Output : Using TRY_PARSE Function 2012-02-06 00:00:00.000 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 65. Security Classification Business Classification PARSE Conversion Function- 3 /*Detecting nulls with TRY_PARSE */ SELECT CASE WHEN TRY_PARSE('Friday, 88 December 2011' AS datetime USING 'en-US') IS NULL THEN 'True' ELSE 'False' END AS Result Output : Result True Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 66. Security Classification Business Classification TRY_PARSE Conversion Function • TRY_PARSE to PARSE is like TRY_CONVERT is to CONVERT • Convert any string value to a Numeric or to a Date and Time format. • if cannot convert a passed value to Numeric or to a Date and Time format, then it will return a NULL value rather than failing the entire TSQL code Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 67. Security Classification Business Classification TRY_PARSE Conversion Function- 1 /* Using PARSE Function to Convert String to INT */ SELECT TRY_PARSE('ONE' AS INT) AS [Using TRY_PARSE Function] Output : Using TRY_PARSE Function NULL /* Using PARSE Function to Convert String to Date Time */ SELECT TRY_PARSE('02/06/2012' AS datetime) AS [Using TRY_PARSE Function] GO Output : Using TRY_PARSE Function 2012-02-06 00:00:00.000 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 68. Security Classification Business Classification TRY_PARSE Conversion Function- 2 /*Detecting nulls with TRY_PARSE */ SELECT CASE WHEN TRY_PARSE('Friday, 88 December 2011' AS datetime USING 'en-US') IS NULL THEN 'True' ELSE 'False' END AS Result Output : Result True Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 69. Security Classification Business Classification TRY_CONVERT Conversion Function • Takes the values passed to it and tries to convert it to a specified Data Type. • When the input value isn’t convertible, instead of generating an error (like CONVERT ) , returns a NULL • If conversion is successful then it will return the value as the specified data type. Otherwise it will return a NULL value. • If conversion that is explicitly not permitted, then TRY_CONVERT fails with an error. • Useful is in dynamic schema scenarios: – WHERE datatype = 'INT' AND CAST(val AS INT) > 10 • With CAST/CONVER can fail on a conversion error because SQL Server may try conversion before evaluating the left expression. • With TRY_CONVERT avoid such failures as follows : – WHERE datatype = 'INT' AND TRY_CONVERT(INT,val) > 10 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 70. Security Classification Business Classification TRY_CONVERT Conversion Function- 1 /* CONVERT returns error */ SELECT CONVERT(float,'test') /* Output : Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to float. /* TRY_CONVERT returns null */ SELECT CASE WHEN TRY_CONVERT(float,'test') IS NULL THEN 'Cast failed' ELSE 'Cast succeeded' END AS Result; GO output: Result ------------ Cast failed Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 71. Security Classification Business Classification TRY_CONVERT Conversion Function- 2 SELECT TRY_CONVERT(INT,289) AS result1, TRY_CONVERT(INT, 'Kuki') AS result2; output: Result1 result2 289 NULL /* TRY_CONVERT fails with an error Because an integer cannot be cast into an xml data type */ SELECT TRY_CONVERT(xml, 4) AS Result; Output is error: Msg 529, Level 16, State 2, Line 13 Explicit conversion from data type int to xml is not allowed. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 72. Security Classification Business Classification TRY_CONVERT Conversion Function- 3 /* TRY_CONVERT succeeds. This example demonstrates that the expression must be in the expected format. */ SET DATEFORMAT mdy; SELECT TRY_CONVERT(datetime2, '12/31/2010') AS Result; GO output Result ---------------------------------- 2010-12-31 00:00:00.0000000 (1 row(s) affected) Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 73. Security Classification Business Classification CHOOSE Logical Function • Return the item at the specified index from the list of values which are available. • In this example we have 3 values and we are asking to select the second value in the list which is "SEQUENCE". SELECT 'New SQL Server Release feature' = CHOOSE(2, 'RESULT SETS', 'SEQUENCE','WINDOWS FUNCTION') Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 74. Security Classification Business Classification IIF Logical Function • Syntax – IIF ( boolean_expression, true_value, false_value ). • IIF is a shorthand way for writing a CASE statement. • Since CASE statements can be nested only up to the level of 10, IIF statements can also be nested only up to the maximum level of 10 /*A. Simple IIF example*/ DECLARE @a int = 2; DECLARE @b int = 40; SELECT IIF ( @a > @b, 'TRUE', 'FALSE' ) AS Result; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 75. Security Classification Business Classification IIF Logical Function-1 /* B. IIF with NULL constants */ SELECT IIF ( 45 > 30, NULL, NULL ) AS Result /* C. IIF with NULL parameters*/ DECLARE @P INT = NULL; DECLARE @S INT = NULL; SELECT IIF ( 45 > 30, @p, @s ) AS Result; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 76. Security Classification Business Classification IIF Logical Function-2 /* Let’s execute the below TSQL code which will use the IIF function to return the results. The logic is if the product Code is between 0 and 3 then return Mechanics Part otherwise return Electronic Part. */ SELECT prd_type_id , IIF(prd_type_id Between 1 AND 3 ,'Mechanics Part','Electronic Part') AS Part_Type FROM [dbo].[total_sales] Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 77. Security Classification Business Classification New FORMAT Function • The FORMAT function, returns a returns a value formatted with the specified format and optional culture • Syntax – FORMAT ( value, format [, culture ] ) • The format argument must contain a valid .NET Framework format string: – Standard format string (for example, "C" or "D") – or a pattern of custom characters for dates and numeric values (for example, "MMMM dd, yyyy (dddd)"). • culture Optional nvarchar argument specifying a culture. • If the culture argument is not provided, then the language of the current session is used. • This language is set either implicitly, or explicitly by using the SET LANGUAGE statement. • culture accepts any culture supported by the .NET Framework as an argument; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 78. Security Classification Business Classification New FORMAT Function- 1 DECLARE @ProductCost INT = 261; SELECT FORMAT ( @ProductCost, 'c', 'en-IN' ) AS [Using 'en-IN' Culture]; SELECT FORMAT ( @ProductCost, 'c', 'de-DE' ) AS [Using 'de-DE' Culture]; GO output: Using 'en-IN' Culture Rs. 261.00 Using 'de-DE' Culture 261,00 € Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 79. Security Classification Business Classification New FORMAT Function- 2 DECLARE @TodaysDate DATETIME = GETDATE() SELECT FORMAT ( @TodaysDate, 'd', 'en-US' ) AS [Using 'en-US' Culture] SELECT FORMAT ( @TodaysDate, 'd', 'de-DE' ) AS [Using 'de-DE' Culture] GO output: Using 'en-US' Culture 2/3/2012 Using 'de-DE' Culture 03.02.2012 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 80. Security Classification Business Classification New CONCAT Function • Returns a string that is the result of concatenating two or more string values . • Implicitly coverts all arguments to string types and then concatenate the inputs. • Requires a minimum of two input values else the concatenation fails. • If all the arguments are null, an empty string of type varchar(1) is returned • The return type depends on the type of the arguments. The following table illustrates the mapping. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 81. Security Classification Business Classification New CONCAT Function- 1 /*Use CONCAT function with a PRINT statement*/ SET NOCOUNT ON SELECT CONCAT('Lecture on MSSQL 2012 By David Itshak ', GETDATE()) as result GO SET NOCOUNT OFF output: result Lecture on MSSQL 2012 By David Itshak Feb 3 2012 1:35AM Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 82. Security Classification Business Classification New CONCAT Function- 2 /* Using CONCAT with NULL values */ CREATE TABLE #temp ( emp_name nvarchar(200) NOT NULL, emp_middlename nvarchar(200) NULL, emp_lastname nvarchar(200) NOT NULL ); INSERT INTO #temp VALUES( 'Name', NULL, 'Lastname' ); SELECT CONCAT( emp_name, emp_middlename, emp_lastname ) AS Result FROM #temp; drop table #temp; GO Output : Result ------------------ NameLastname Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 83. Security Classification Business Classification New Date and Time Functions • DATEFROMPARTS Function • TIMEFROMPARTS Function • DATETIMEFROMPARTS Function • DATETIME2FROMPARTS Function • SMALLDATETIMEFROMPARTS Function • DATETIMEOFFSETFROMPARTS Function • EOMONTH Function Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 84. Security Classification Business Classification Using TIMEFROMPARTS Function • Returns a full time value . • SYNTAX: TIMEFROMPARTS (HOUR, MINUTE, SECONDS, FRACTIONS, PRECISION) • Fractions argument depends on the precision argument. • Examples: – When fractions= 5 and precision= 1, then fractions = 5/10^1 of a second. – When fractions =50 and precision=2, then fractions=50/10^2=50/100 of a second. – When fractions=500 and precision=3, then fractions=500/10^3=500/1000 of a second. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 85. Security Classification Business Classification Using TIMEFROMPARTS Function-1 DECLARE @HOUR INT = 12 , @MINUTE INT = 37 , @SECONDS INT = 42 SELECT TIMEFROMPARTS (@HOUR, @MINUTE, @SECONDS, 500, 3) AS [Result Using TIMEFROMPARTS Function] GO Output : Result Using TIMEFROMPARTS Function ==================================== 12:37:42.500 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 86. Security Classification Business Classification EXECUTE Statement WITH RESULT SETS argument • The EXECUTE statement can now specify the metadata returned from the statement by using the WITH RESULT SETS argument. • A limitation in the previous versions of SQL Server that whenever you wanted to change a Column Name or a Data Type within the result set of a Stored Procedure you ended up making changes to all the references within a Stored Procedure. • This feature can be very useful when executing a stored procedure in SSIS where you can execute the Stored Procedure with the required columns names and appropriate data types. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 87. Security Classification Business Classification EXECUTE Statement WITH RESULT SETS argument- • Example : Using EXECUTE to redefine a two result sets • When executing a statement that returns more than one result set, define each expected result set. The following example 2 creates a procedure that returns two result sets. Then the procedure is executed using the WITH RESULT SETS clause, and specifying two result set definitions • The first result set doesn't change the names or data types and the second result set we have changed the column names and data types using this new feature. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 88. Security Classification Business Classification EXECUTE Statement WITH RESULT SETS argument-2 USE tempdb GO IF EXISTS (SELECT * FROM sys.objects WHER object_id = OBJECT_ID(N'[dbo].[UsingWithResultSets]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[UsingWithResultSets] GO CREATE PROCEDURE UsingWithResultSets AS BEGIN SELECT top(2) job_id , CONCAT([first_name] , ' ' , [last_name]) AS Name, [division_id] FROM [dbo].[employees]; SELECT top(2) [prd_type_id], CONCAT( [year] ,'-', [month]) Q, [amount] FROM [dbo].[total_sales] END GO Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 89. Security Classification Business Classification EXECUTE Statement WITH RESULT SETS argument-3 /* Execute Stored Procedure */ EXEC UsingWithResultSets GO /* Using WITH Result Sets Feature Of SQL Server 2012 */ EXEC UsingWithResultSets WITH RESULT SETS ( ( job_id VARCHAR(5), employeen_Name VARCHAR(200), division_id VARCHAR(10) ) , ( prd_type_idntity int, quar VARCHAR(50), amount_of_prod numeric(9,3) ) ) Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 90. Security Classification Business Classification Legacy Error Handling • In SQL Server 2000 @@error system function. • An @@error value of 0 means no error occurred. • limitations : automatically reset to 0 after every successful statement. • you cannot have any statements between the code that you expect might produce an exception and the code that checks the value of @@error. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 91. Security Classification Business Classification Legacy Error Handling- 1 USE [tempdb] GO IF OBJECT_ID('dbo.error_test') IS NOT NULL DROP TABLE [dbo].[error_test] GO create table [dbo].[error_test] (a int NOT NULL , b int NOT NULL); GO insert into [error_test] (b) values (1); GO IF OBJECT_ID('dbo.test_error') IS NOT NULL DROP procedure dbo.test_error; GO CREATE PROCEDURE dbo.test_error (@e int OUTPUT) AS BEGIN INSERT INTO [error_test] (b) values (1); PRINT N'Error code in procedure = ' + CAST(@@error AS nvarchar(10)); SET @e = @@error; END Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 92. Security Classification Business Classification Legacy Error Handling- 2 DECLARE @ret int, @e int; EXEC @ret = dbo.test_error @e OUTPUT; PRINT N'Returned error code = ' + CAST(@e AS nvarchar(10)); PRINT N'Return value = ' + CAST(@ret AS nvarchar(10)); Output : Msg 515, Level 16, State 2, Procedure test_error, Line 4 Cannot insert the value NULL into column 'a', table 'tempdb.dbo.error_test'; column does not allow nulls. INSERT fails. The statement has been terminated. Error code in procedure = 515 Returned error code = 0 Return value = -6 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 93. Security Classification Business Classification Try...Catch Exception Handling • Supported by SQL Server 2008 • Common in other modern programming languages. • The T-SQL statements in the BEGIN TRY...END TRY block execute normally. • If the block completes without error, the T-SQL statements between the BEGIN CATCH...END CATCH block are skipped. • If an exception is thrown by the statements in the TRY block, control transfers tothe statements in the BEGIN CATCH...END CATCH block. • CATCH Block Functions : – ERROR_LINE() Returns the line number on which the exception occurred – ERROR_MESSAGE() Returns the complete text of the generated error message – ERROR_PROCEDURE() Returns the name of the SP or trigger where the error occurred – ERROR_NUMBER() Returns the number of the error that occurred Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 94. Security Classification Business Classification Try...Catch Exception Handling-1 IF OBJECT_ID('dbo.test_error1') IS NOT NULL DROP procedure dbo.test_error1; GO CREATE PROCEDURE dbo.test_error1 (@e int OUTPUT) AS BEGIN SET @e = 0; BEGIN TRY insert into [error_test] (b) values (1); END TRY BEGIN CATCH SET @e = ERROR_NUMBER(); PRINT N'Error Code = ' + CAST(@e AS nvarchar(10)); PRINT N'Error Procedure = ' + ERROR_PROCEDURE(); PRINT N'Error Message = ' + ERROR_MESSAGE(); END CATCH END GO Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 95. Security Classification Business Classification Try...Catch Exception Handling-2 DECLARE @ret int, @e int; EXEC @ret = dbo.test_error1 @e OUTPUT; PRINT N'Error code = ' + CAST(@e AS nvarchar(10)); PRINT N'Return value = ' + CAST(@ret AS nvarchar(10)); Output : Error Code = 515 Error Procedure = test_error1 Error Message = Cannot insert the value NULL into column 'a', table 'tempdb.dbo.error_test'; column does not allow nulls. INSERT fails. Error code = 515 Return value = -6 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 96. Security Classification Business Classification THROW statement - SQL Server 2012 • The THROW statement can be used to raise an exception and transfer execution to a CATCH block of a TRY…CATCH construct. • THROW command is different RAISERROR command : – It is not mandatory to have a message available in sys.messages system object if you are using the error number or error ID. – By default the exception thrown using the THROW command has a severity level of 16 and you cannot change it. – You cannot use print style formatting with the THROW command although you can use the FORMATMESSAGE function to achieve the same results. – When you re-throw the exception with the THROW keyword the original error number and line number is preserved unlike the RAISERROR command where it is overwritten. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 97. Security Classification Business Classification THROW statement - SQL Server 2012 - 1 IF OBJECT_ID('dbo.test_error2') IS NOT NULL DROP procedure dbo.test_error2; GO CREATE PROCEDURE dbo.test_error2 (@e int OUTPUT) AS BEGIN SET @e = 0; BEGIN TRY -- Force column does not allow nulls . insert into [error_test] (b) values (1); END TRY BEGIN CATCH PRINT 'In catch block.'; THROW; END CATCH END GO Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 98. Security Classification Business Classification THROW statement - SQL Server 2012 - 2 DECLARE @ret int, @e int; EXEC @ret = dbo.test_error2 @e OUTPUT; Output : Msg 515, Level 16, State 2, Procedure test_error2, Line 6 Cannot insert the value NULL into column 'a', table 'tempdb.dbo.error_test'; column does not allow nulls. INSERT fails. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 99. Security Classification Business Classification SQL Server 2012 Sequence Numbers CREATE SEQUENCE [schema_name . ] sequence_name [ <sequence_property_assignment> [ ,…n ] ] [;] <sequence_property_assignment>::= { [ AS { built_in_integer_type | user-defined_integer_type } ] | START WITH <constant> | INCREMENT BY <constant> | { MINVALUE <constant> | NO MINVALUE } | { MAXVALUE <constant> | NO MAXVALUE } | { CYCLE | NO CYCLE } | { CACHE [<constant> ] | NO CACHE } } Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 100. Security Classification Business Classification SQL Server 2012 Sequence Numbers- • CREATE SEQUENCE statement: – Specify the schema name – Specify the sequence name – Sequence type : – Any of the built-in integer types: – tinyint, smallint, int, bigint, decimal or numeric – decimal or numeric require a scale of 0 – user-defined type that is based on one of the built-in integer types – Default sequence type is INT – Use START WITH <integer constant> to specify the first sequence number to be assigned; this can be a negative number – INCREMENT BY <integer constant> determines the next value assigned by the sequence number; this can be a positive or negative value but not 0 Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 101. Security Classification Business Classification SQL Server 2012 Sequence Numbers-2 • CREATE SEQUENCE statement: – MINVALUE <integer constant> and MAXVALUE <integer constant> provide the bounds for the sequence number; the default for MINVALUE is 0 for a tinyint and the smallest negative number for the type of the sequence number; the default for MAXVALUE is the maximum value for the type of the sequence number – Specify CYCLE to restart the sequence number at the MINVALUE after the MAXVALUE is reached – Specify NO CYCLE to throw an exception after the MAXVALUE is reached rather than restarting with the MINVALUE – Use CACHE <integer constant> to retrieve a sequential block of sequence numbers – Use the NEXT VALUE FOR statement to assign and retrieve the next value for a sequence number. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 102. Security Classification Business Classification SQL Server 2012 Sequence Numbers-3 • Caching – SQL server supports caching option for sequence (like Oracle) – Improved performance by minimizing disk access – NO CACHE to prevent caching – CACH for caching • Miscellaneous Notes – To retrieve a range of values from a sequence number, use the sys.sp_sequence_get_range stored procedure. – To retrieve information about sequence numbers, use the sys.sequences stored procedure – Other related Transact-SQL commands are ALTER SEQUENCE and DROP SEQUENCE. – The NEXT VALUE FOR statement is not run as part of the current transaction; a rollback does not affect the sequence number(s) already assigned. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 103. Security Classification Business Classification SEQUENCE example -1 USE tempdb ; GO IF OBJECT_ID('test.seq1','SO') IS NOT NULL DROP SEQUENCE test.seq1; GO IF OBJECT_ID('test.RangeSeq','SO') IS NOT NULL DROP SEQUENCE test.seq1; GO IF OBJECT_ID('test.seq1','SO') IS NOT NULL DROP SEQUENCE test.seq3; GO IF OBJECT_ID('test.tbl_seq','T') IS NOT NULL DROP TABLE test.tbl_seq; GO IF EXISTS (select * from sys.schemas where name='test' ) BEGIN drop schema test; END ; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 104. Security Classification Business Classification SEQUENCE example -2 -- Create a schema named test CREATE SCHEMA test; GO -- Create a sequence called dbo.Seq1: CREATE SEQUENCE test.Seq1 AS INT START WITH 1 INCREMENT BY 1; -- You invoke the following code in attempt to generate two sequence values: SELECT NEXT VALUE FOR test.Seq1 AS val1, NEXT VALUE FOR test.Seq1 AS val2; /* This code doesn’t generate two different sequence values but rather just one, a one value in both result columns val1 and val2: val1 val2 ----------- ----------- 1 1 All expressions that appear in the same logical phase (the SELECT in this case conceptually evaluated at the same point in time */ Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 105. Security Classification Business Classification * SEQUENCE example -3 • Retrieving a range of sequence values using sp_sequence_get_range • To request a new range of sequence values. • Provide the sequence name and the range size as input. • Output Parameters: • The first value in the range • The last value • The number of times the sequence cycled (If allowed) • When asking for a new range exceeding the maximum (or minimum , if increment is negative)you will get error 11732. /* Example : The following statement gets four sequence numbers from the Test.RangeSeq sequence object and returns the first of the numbers to the user. */ CREATE SEQUENCE test.RangeSeq AS int START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 25 CYCLE CACHE 10; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 106. Security Classification Business Classification SEQUENCE example -4 /* Get sequence metadata */ SELECT current_value, is_cached,cache_size from sys.sequences WHERE OBJECT_ID=OBJECT_ID('test.RangeSeq') current_value is_cached cache_size ------------- --------- ---------- 1 1 10 DECLARE @range_first_value sql_variant , @range_first_value_output sql_variant ; EXEC sp_sequence_get_range @sequence_name = N'Test.RangeSeq' , @range_size = 4 , @range_first_value = @range_first_value_output OUTPUT ; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 107. Security Classification Business Classification * SEQUENCE example -5 Returning all output parameters ================================= The following example returns all the output values from the sp_sequence_get_range procedure. */ DECLARE @FirstSeqNum sql_variant , @LastSeqNum sql_variant , @CycleCount int , @SeqIncr sql_variant , @SeqMinVal sql_variant , @SeqMaxVal sql_variant ; EXEC sys.sp_sequence_get_range @sequence_name = N'Test.RangeSeq' , @range_size = 5 , @range_first_value = @FirstSeqNum OUTPUT , @range_last_value = @LastSeqNum OUTPUT , @range_cycle_count = @CycleCount OUTPUT , @sequence_increment = @SeqIncr OUTPUT , @sequence_min_value = @SeqMinVal OUTPUT , @sequence_max_value = @SeqMaxVal OUTPUT ; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 108. Security Classification Business Classification SEQUENCE example -6 -- The following statement returns the output values SELECT @FirstSeqNum AS FirstVal , @LastSeqNum AS LastVal , @CycleCount AS CycleCount , @SeqIncr AS SeqIncrement , @SeqMinVal AS MinSeq , @SeqMaxVal AS MaxSeq ; /* output : FirstVal LastVal CycleCount SeqIncrement MinSeq MaxSeq 5 9 0 1 1 25 FirtsVal is 5 because at first run @range_size = 4 . LastVal is 9 because at second run @range_size = 5 so LastVal is 4+5=9 */ Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 109. Security Classification Business Classification SEQUENCE example -7 /* Sequence Number Examples The following examples will show the basic use cases for a sequence number. The examples use the following schema and table: */ USE [tempdb] GO CREATE TABLE test.tbl_seq ( SeqNumber INT PRIMARY KEY , Title VARCHAR(50) ) GO Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 110. Security Classification Business Classification SEQUENCE example -8 /* Create a sequence number in the test schema to assign tip numbers on the test web site: */ CREATE SEQUENCE test.seq3 AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1 GO /* Get the next tip number and assign to a T-SQL variable (you could use @TipNumber in the VALUES clause of one or more INSERT statements): */ DECLARE @TipNumber As INT = NEXT VALUE FOR test.seq3 PRINT @TipNumber Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 111. Security Classification Business Classification SEQUENCE example -9 /* Insert a row into a table and get the next value for the TipNumber: */ INSERT INTO test.tbl_seq (SeqNumber, Title) VALUES (NEXT VALUE FOR test.seq3, 'SQL 2012 - Sequence Numbers DEMO'); select * from test.tbl_seq; DROP SEQUENCE test.seq1; DROP SEQUENCE test.seq3; DROP SEQUENCE test.RangeSeq; DROP TABLE test.tbl_seq; drop schema test ; Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 112. Security Classification Business Classification SQL Server 2012 Sequence Numbers-4 • A column in a table can have the IDENTITY property which provides for automatically assigning the next value to the column on insert. • Compare sequence number with the identity column. – An identity column is tied to a table; you have to insert a row in order to get the next value. – You can use the NEXT VALUE FOR statement to assign and retrieve the next value for a sequence number; it is not tied to a particular column in a table and you do not have to insert a row to get the next value. – A sequence number can be used to provide an automatically generated number that is used over multiple tables; the identity column is tied to a single table. – identity column : Specify the starting value and increment with an ; – sequence number : you can specify the starting value, increment, minimum, maximum, caching, and whether to recycle the values – The NEXT VALUE FOR statement includes an OVER clause allowing you to assign sequence numbers based on an ORDER BY; an identity column does not have this capability. Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 113. Security Classification Business Classification SQL Server 2012 Sequence Numbers-5 • Choose a sequence number in these scenarios: – You want the next value without having to insert a row into a table. – You want to use the value across multiple tables. – You want to automatically recycle the values. • Choose an IDENTITY in these scenarios: – You want to assign the next value on insert for a single table. • Currently , the size of IDENTITY is a hard coded value of 10. • the default cache size is 50 for sequence . • If the system shuts unexpectedly , you lose the remaining cached values and end up with a gap .. • with IDENTITY , during recovery it scans the log to find out last used IDENTITY , so you end up not losing the remaining cached values Proprietary of Rafael - Advanced Defense Systems.Ltd.
  • 114. Security Classification Business Classification Discussion, questions & answers Proprietary of Rafael - Advanced Defense Systems.Ltd.