• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Fast transition to sql server 2012 from mssql 2005 2008 for  developers - David Yitzhak
 

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

on

  • 2,306 views

 

Statistics

Views

Total Views
2,306
Views on SlideShare
1,962
Embed Views
344

Actions

Likes
1
Downloads
43
Comments
0

1 Embed 344

http://www.sqlserver.co.il 344

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

    • Security Classification Business ClassificationFast Transition To SQL 2012 fromMSSQL 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 2010RAFDOCS-#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 ClassificationPrevious Lectures-1 Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification Previous Lectures-2Database 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 Sever11GR2, 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 DRPshaked19@gmail.comProprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business ClassificationPrevious 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 scriptScript Name : fast_transition_to_mssql_2012.sqlAuthor:Dadid ItshakEmail:cimid@rafael.co.ilDate:Feb 6th 2012Content :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 EnhancedSource : Window Functions in SQL Server: Part 2-The Frame ,24 November 2011by 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 DescriptionUSE TempDBGOIF OBJECT_ID(Tab1) IS NOT NULLDROP TABLE Tab1GOCREATE TABLE Tab1 (Col1 INT)GOINSERT 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 distributionProperties : 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 groupSELECT 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 TableThe total_sales table stores the sum of all the sales by dollaramount 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 ClassificationUsing the RANK() and DENSE_RANK() Functions -1SELECTprd_type_id, SUM(amount),RANK() OVER (ORDER BY SUM(amount) DESC) AS rank,DENSE_RANK() OVER (ORDER BY SUM(amount) DESC) AS dense_rankFROM total_salesWHERE year = 2012AND amount IS NOT NULLGROUP BY prd_type_idORDER 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 ClassificationControlling 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:SELECTprd_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_rankFROM total_salesWHERE year = 2012GROUP BY prd_type_idORDER BY prd_type_id; Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business ClassificationUsing 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 :SELECTprd_type_id, month, SUM(amount) AS SUM_Amount ,RANK() OVER (PARTITION BY month ORDER BY SUM(amount) DESC) AS rankFROM total_salesWHERE year = 2012AND amount IS NOT NULLGROUP BY prd_type_id, monthORDER 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:SELECTprd_type_id, SUM(amount) SUM_Amount,RANK() OVER (ORDER BY SUM(amount) DESC) AS rankFROM total_salesWHERE year = 2012GROUP 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 :SELECTprd_type_id, emp_id, SUM(amount)as SUM_Amount,RANK() OVER (ORDER BY SUM(amount) DESC) AS rankFROM total_salesWHERE year = 2012GROUP 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 examplesubtotals for product type id and employee id .SELECTprd_type_id, emp_id, SUM(amount) SUM_Amount,RANK() OVER (ORDER BY SUM(amount) DESC) AS rankFROM total_salesWHERE year = 2012GROUP 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:SELECTprd_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_rankFROM total_salesWHERE year = 2012GROUP BY prd_type_idORDER BY prd_type_id Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification Using the CUME_DIST() and PERCENT_RANK() Functions -SELECTprd_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_rankFROM total_salesWHERE year = 2012GROUP BY prd_type_idORDER 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 bucketsSELECTprd_type_id, SUM(amount) SUM_Amount,NTILE(4) OVER (ORDER BY SUM(amount) DESC) AS ntileFROM total_salesWHERE year = 2012AND amount IS NOT NULLGROUP BY prd_type_idORDER 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 :SELECTprd_type_id, SUM(amount) SUM_Amount,ROW_NUMBER() OVER (ORDER BY SUM(amount) DESC) AS row_numberFROM total_salesWHERE year = 2012GROUP BY prd_type_idORDER 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.6SELECTPERCENTILE_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_discFROM total_salesWHERE year = 2012GROUP 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:SELECTmonth, SUM(amount) AS month_amount,SUM(SUM(amount)) OVER(ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)AS cumulative_amountFROM total_salesWHERE year = 2012GROUP BY monthORDER 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 12SELECTmonth, SUM(amount) AS month_amount,SUM(SUM(amount)) OVER(ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AScumulative_amountFROM total_salesWHERE year = 2012AND month BETWEEN 6 AND 12GROUP BY monthORDER 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 currentmonth and the previous 3 months:SELECTmonth, SUM(amount) AS month_amount,AVG(SUM(amount)) OVER(ORDER BY month ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)AS moving_averageFROM total_salesWHERE year = 2012GROUP BY monthORDER BY month;Example :month 11 moving_average= 209262.032500= (160221.98+264480.79+199609.68+212735.68)/4month 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:SELECTmonth, SUM(amount) AS month_amount,AVG(SUM(amount)) OVER(ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)AS moving_averageFROM total_salesWHERE year = 2012GROUP BY monthORDER BY month;Example :month 5 moving average=(175998.80+154349.44+124951.36)/3=151766.533333month 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 ? - 1AVG(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 ClassificationGetting 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 amountSELECTmonth, 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_amountFROM total_salesWHERE year = 2012GROUP BY monthORDER 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).SELECTmonth, 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_amountFROM total_salesWHERE year = 2012AND month <= 3GROUP BY month, prd_type_idORDER 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:SELECTmonth, 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_amountFROM total_salesWHERE year = 2012GROUP BY monthORDER 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 ClassificationUsing 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 ClassificationUsing 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 ClassificationUsing 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]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 EnglishSELECT PARSE(02/06/2012 AS datetime2) AS ResultOutput:Result2012-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 FunctionNULL/* Using PARSE Function to Convert String to Date Time */SELECT TRY_PARSE(02/06/2012 AS datetime) AS [Using TRY_PARSE Function]GOOutput :Using TRY_PARSE Function2012-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 FalseENDAS ResultOutput :ResultTrue 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 FunctionNULL/* Using PARSE Function to Convert String to Date Time */SELECT TRY_PARSE(02/06/2012 AS datetime) AS [Using TRY_PARSE Function]GOOutput :Using TRY_PARSE Function2012-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 FalseENDAS ResultOutput :ResultTrue 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 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 succeededEND AS Result;GOoutput:Result------------Cast failed Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification TRY_CONVERT Conversion Function- 2SELECT 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. 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;GOoutputResult----------------------------------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 IIFfunction to return the results.The logic is if the product Code is between 0 and 3 thenreturn Mechanics Part otherwise return Electronic Part.*/SELECT prd_type_id , IIF(prd_type_id Between 1 AND 3 ,MechanicsPart,Electronic Part) AS Part_TypeFROM [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- 1DECLARE @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.00Using de-DE Culture261,00 € Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification New FORMAT Function- 2DECLARE @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/2012Using de-DE Culture03.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 ONSELECT CONCAT(Lecture on MSSQL 2012 By David Itshak , GETDATE()) as resultGOSET NOCOUNT OFFoutput:resultLecture 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 ResultFROM #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-1DECLARE @HOUR INT = 12, @MINUTE INT = 37, @SECONDS INT = 42SELECT TIMEFROMPARTS (@HOUR, @MINUTE, @SECONDS, 500, 3) AS [Result Using TIMEFROMPARTS Function]GOOutput :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 doesnt 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-2USE tempdbGOIF EXISTS (SELECT * FROM sys.objects WHERobject_id = OBJECT_ID(N[dbo].[UsingWithResultSets]) ANDtype in (NP, NPC))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 Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification EXECUTE Statement WITH RESULT SETS argument-3/* 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) )) 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- 1USE [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);GOinsert into [error_test] (b) values (1);GOIF 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 NError 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- 2DECLARE @ret int,@e int;EXEC @ret = dbo.test_error @e OUTPUT;PRINT NReturned error code = + CAST(@e AS nvarchar(10));PRINT NReturn 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 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-1IF 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 NError Code = + CAST(@e AS nvarchar(10));PRINT NError Procedure = + ERROR_PROCEDURE();PRINT NError Message = + ERROR_MESSAGE();END CATCHENDGO Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification Try...Catch Exception Handling-2DECLARE @ret int,@e int;EXEC @ret = dbo.test_error1 @e OUTPUT;PRINT NError code = + CAST(@e AS nvarchar(10));PRINT NReturn value = + CAST(@ret AS nvarchar(10));Output :Error Code = 515Error Procedure = test_error1Error Message = Cannot insert the valueNULL into column a, table tempdb.dbo.error_test;column does not allow nulls. INSERT fails.Error code = 515Return 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 - 1IF 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 Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification THROW statement - SQL Server 2012 - 2DECLARE @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. Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification SQL Server 2012 Sequence NumbersCREATE 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 -1USE tempdb ;GOIF OBJECT_ID(test.seq1,SO) IS NOT NULLDROP SEQUENCE test.seq1;GOIF OBJECT_ID(test.RangeSeq,SO) IS NOT NULLDROP SEQUENCE test.seq1;GOIF OBJECT_ID(test.seq1,SO) IS NOT NULLDROP SEQUENCE test.seq3;GOIF OBJECT_ID(test.tbl_seq,T) IS NOT NULLDROP TABLE test.tbl_seq;GOIF EXISTS (select * from sys.schemas where name=test )BEGINdrop schema test;END ; Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification SEQUENCE example -2-- Create a schema named testCREATE 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, aone 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 caseconceptually 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 theTest.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 10DECLARE @range_first_value sql_variant , @range_first_value_output sql_variant ;EXEC sp_sequence_get_range@sequence_name = NTest.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 -5Returning all output parameters================================= The following example returns all the output values from thesp_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 = NTest.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 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 25FirtsVal is 5 because at first run @range_size = 4 .LastVal is 9 because at second run @range_size = 5 so LastValis 4+5=9*/ Proprietary of Rafael - Advanced Defense Systems.Ltd.
    • Security Classification Business Classification SEQUENCE example -7/* Sequence Number ExamplesThe following examples will show the basic use cases for asequence number. The examples use the following schema andtable:*/USE [tempdb]GOCREATE 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 tipnumbers 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 (youcould use @TipNumber in the VALUES clause of one or moreINSERT statements):*/DECLARE @TipNumber As INT = NEXT VALUE FOR test.seq3PRINT @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 theTipNumber:*/INSERT INTO test.tbl_seq (SeqNumber, Title)VALUES (NEXT VALUE FOR test.seq3, SQL 2012 - Sequence NumbersDEMO);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 ClassificationDiscussion, questions & answers Proprietary of Rafael - Advanced Defense Systems.Ltd.