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

114
Security Classification Business Classification Proprietary of Rafael - Advanced Defense Systems.Ltd. ךךךךך ךךךךךךך1 Fast Transition To SQL 2012 from MSSQL 2005/2008 For Developers ךךך ךךךך ךךך"ך – ךךךךךך ךךךךך ךךךךךךך ךך"ך[email protected] 6.2.2012

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd. 1סימוכיןתאריך

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

דוד יצחק רפא"ל – מערכות לחימה מתקדמות בע"מ

[email protected]

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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)

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

Previous Lectures-1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd. 6סימוכיןתאריך

Database Mirror for the exceptional DBA דוד יצחק

רפא"ל – מערכות לחימה מתקדמות בע"מ[email protected]

4.4.2011

Previous Lectures-2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd. 7סימוכיןתאריך

When ODAC ,Oracle Sever 11GR2, Win HPC Server 2008

Come together …

David ItshakRafael Advanced Defense [email protected]

Previous Lectures-3

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.8

Previous Lectures-4 של DRPכשלים בתהליכי

מערכות מחשוב

דוד יצחק[email protected]

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

Previous Lectures-5

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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”

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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:[email protected]

Date:Feb 6th 2012

Content :

Modification History:

Date Who What

============= ===============

*/

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

USE TempDBGOIF OBJECT_ID('Tab1') IS NOT NULLDROP TABLE Tab1GOCREATE TABLE Tab1 (Col1 INT)GO

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

Analytic Functions Description

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 Tab1GOSELECT Col1, DENSE_RANK() OVER(ORDER BY Col1 DESC) AS "DENSE_RANK" FROM Tab1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 :

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

The percentile rank of a score

is the percentage of scores in its

frequency distribution that are

the same or lower than it. For

example, a test score that is

greater than 75% of the scores

of people taking the test is said

to be at the 75th percentile.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/*The Example TableThe 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 );

Using the Analytic Functions -Example Table

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/*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]

1Using the Analytic Functions -Example Table

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

Using the RANK() and DENSE_RANK() Functions -1SELECT

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

905081.84478270.91402751.16186381.2200

0.2

0.4

0.6

0.8

1

1.2

1 -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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.6SELECT

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 :

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 .

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

WHERE year = 2003

AND amount IS NOT NULL

GROUP BY prd_type_id

ORDER BY prd_type_id;

RANK PERCENT_RANK2 0.25

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Output:Result2012-02-06 00:00:00.0000000

PARSE Conversion Function- 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

Output :Using TRY_PARSE FunctionNULL

/* 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 Function2012-02-06 00:00:00.000

PARSE Conversion Function- 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/*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'ENDAS Result

Output : ResultTrue

PARSE Conversion Function- 3

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

Output :Using TRY_PARSE FunctionNULL

/* 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 Function2012-02-06 00:00:00.000

TRY_PARSE Conversion Function- 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/*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'ENDAS Result

Output : ResultTrue

/*

TRY_PARSE Conversion Function- 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/* CONVERT returns error */SELECT CONVERT(float,'test') /* Output :Msg 8114, Level 16, State 5, Line 1Error 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

TRY_CONVERT Conversion Function- 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

Result1 result2289 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 13Explicit conversion from data type int to xml is not allowed.

TRY_CONVERT Conversion Function- 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/* 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)

TRY_CONVERT Conversion Function- 3

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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_TypeFROM [dbo].[total_sales]

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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];GOoutput:

Using 'en-IN' CultureRs. 261.00

Using 'de-DE' Culture261,00 €

New FORMAT Function- 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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]GOoutput:

Using 'en-US' Culture2/3/2012

Using 'de-DE' Culture03.02.2012

New FORMAT Function- 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

New CONCAT Function- 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/* 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 ResultFROM #temp;drop table #temp;GO Output :

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

New CONCAT Function- 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

New Date and Time Functions• DATEFROMPARTS Function

• TIMEFROMPARTS Function

• DATETIMEFROMPARTS Function

• DATETIME2FROMPARTS Function

• SMALLDATETIMEFROMPARTS Function

• DATETIMEOFFSETFROMPARTS Function

• EOMONTH Function

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

Using TIMEFROMPARTS Function-1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

1-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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

USE tempdbGO

IF EXISTS (SELECT * FROM sys.objects WHERobject_id = OBJECT_ID(N'[dbo].[UsingWithResultSets]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[UsingWithResultSets]GOCREATE PROCEDURE UsingWithResultSetsASBEGIN 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]

ENDGO

EXECUTE Statement WITH RESULT SETS argument-2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/* Execute Stored Procedure */EXEC UsingWithResultSetsGO/* Using WITH Result Sets Feature Of SQL Server 2012 */

EXEC UsingWithResultSetsWITH 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) ))

EXECUTE Statement WITH RESULT SETS argument-3

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

USE [tempdb]GOIF OBJECT_ID('dbo.error_test') IS NOT NULL DROP TABLE [dbo].[error_test]GOcreate 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;GOCREATE PROCEDURE dbo.test_error (@e int OUTPUT)ASBEGININSERT INTO [error_test] (b) values (1);PRINT N'Error code in procedure = ' + CAST(@@error AS nvarchar(10));SET @e = @@error;END

Legacy Error Handling- 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 4Cannot 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 = 515Returned error code = 0Return value = -6

Legacy Error Handling- 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

IF OBJECT_ID('dbo.test_error1') IS NOT NULL DROP procedure dbo.test_error1;GOCREATE PROCEDURE dbo.test_error1 (@e int OUTPUT)ASBEGINSET @e = 0;BEGIN TRYinsert into [error_test] (b) values (1);END TRYBEGIN CATCHSET @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 CATCHENDGO

Try...Catch Exception Handling-1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 = 515Error Procedure = test_error1Error Message = Cannot insert the value NULL into column 'a', table 'tempdb.dbo.error_test'; column does not allow nulls. INSERT fails.Error code = 515Return value = -6

Try...Catch Exception Handling-2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

IF OBJECT_ID('dbo.test_error2') IS NOT NULL DROP procedure dbo.test_error2;GOCREATE PROCEDURE dbo.test_error2 (@e int OUTPUT)ASBEGINSET @e = 0;BEGIN TRY-- Force column does not allow nulls .insert into [error_test] (b) values (1);END TRYBEGIN CATCHPRINT 'In catch block.';THROW;END CATCHENDGO

THROW statement - SQL Server 2012 - 1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

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

THROW statement - SQL Server 2012 - 2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 }

}

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

1-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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

2-SQL Server 2012 Sequence Numbers

• 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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

3-SQL Server 2012 Sequence Numbers• 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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

GO

SEQUENCE example -1

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

-- 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, and returns that 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) are conceptually evaluated at the same point in time */

SEQUENCE example -2

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

*

• 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;

SEQUENCE example -3

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

/* 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 ;

;

SEQUENCE example -4

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

*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 ;

;

SEQUENCE example -5

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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

/*output : FirstVal LastVal CycleCount SeqIncrement MinSeq MaxSeq5 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*/

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

SEQUENCE example -7 /* Sequence Number ExamplesThe 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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

SEQUENCE example -8 /*Create a sequence number in the test schema to assign tip numbers on the test web site:*/CREATE SEQUENCE test.seq3AS INTSTART WITH 1INCREMENT BY 1MINVALUE 1GO/*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.seq3PRINT @TipNumber

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

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 ;

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

4-SQL Server 2012 Sequence Numbers• 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.

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

5-SQL Server 2012 Sequence Numbers• 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

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

Security ClassificationBusiness Classification

Proprietary of Rafael - Advanced Defense Systems.Ltd.

Discussion, questions & answers