New T-SQL Features in SQL Server 2012


Published on

With the introduction of SQL Server 2012 data developers have new ways to interact with their databases. This session will review the powerful new analytic windows functions, new ways to generate numeric sequences and new ways to page the results of our queries. Other features that will be discussed are improvements in error handling and new parsing and concatenating features.

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Data Paging – OFFSET and FETCHAnalytic Window Functions – LAG, LEAD, FIRST_VALUE, LAST_VALUE, PERCENT_RANK, CUME_DIST, PERCENTILE_CONT, PERCENTILE_DISC Conversion Functions – PARSE, TRY_PARSE, TRY_CONVERTLogical Functions – IIF, CHOOSEDate/Time Functions – EOMONTH, FROMPARTSString Functions – CONCAT, FORMATError Handling - THROW
  • New T-SQL Features in SQL Server 2012

    1. 1. Richie Rump @Jorriss
    2. 2. • Sequences• Data Paging• Analytic Window Functions• Conversion Functions• Logical Functions• Date/Time Functions• String Functions
    3. 3. • New construct to generate numeric sequences.• Ability to set min and max values• Cycle will set the current value to the min value when the max value is reached• Future values can be cached minimizing disk IO.
    4. 4. • Returns metadata for the supplied SQL statement.• Useful for determining column/database information for a SQL statement or Stored Procedure.
    5. 5. • If set to 0, no information is returned.• If set to 1, each query is analyzed as if it includes a FOR BROWSE option on the query. This will return base table names as the source column information.• If set to 2, each query is analyzed as if it would be used in preparing or executing a cursor. This will return view names as source column information.
    6. 6. • Allows the changing of column names and data types in the resultset of a stored procedure.• Yes, you can change column names when you call a stored procedure.
    7. 7. • Allows for server side paging.• Not data pages (8K of awesome) but paging like on a web page.• OFFSET AND FETCH are arguments of the ORDER BY clause• OFFSET – Number of rows to skip• FETCH – Number of rows to return
    9. 9. • The OVER clause determines the partitions and order of a rowset before the window function is applied.• WHAT CHU TALKING ABOUT WILLIS?
    10. 10. • PARTITON BY – Similar to GROUP BY but only applies to the window function and not the entire query.• ORDER BY – Specifies the order of the rows in the partition
    11. 11. • No longer need to use a self-join or CTE.• LAG: Access data from previous rows in the result set.• LEAD: Access data from future rows in the result set.
    12. 12. • FIRST_VALUE: Retrieves the first value in a partition.• LAST_VALUE: Retrieves the last value in a partition.
    13. 13. • Calculates a relative rank of a row.• (RANK() – 1) / (Total Rows – 1)
    14. 14. • Calculates the percentage of values less than or equal to the current value in the group.• COUNT(*) OVER (ORDER BY Col1) / Total Count
    15. 15. • Calculates a percentile value• Will interpolate the appropriate value• Can use to find the median – PERCENTILE_CONT(0.5)• CONT stands for continuous
    16. 16. • Calculates a percentile value• Like PERCENTILE_CONT but will select a value that exists in the set.• Can use to find the median – PERCENTILE_CONT(0.5)• DISC stands for discrete distribution
    17. 17. • Attempts to parse a string and convert it to a specified datatype.• Can only convert to a number or datetime• Uses the .Net CLR
    18. 18. • Like PARSE but if an error occurs returns a NULL.• Still uses the .Net CLR.
    19. 19. • Attempts to cast a value into a specified data type. Returns NULL if CONVERT fails.
    20. 20. • Immediate If aka Inline If• Takes a boolean expression and returns one of two values.• Shorthand for CASE.• Has the same limitations as CASE – Can only be nested to 10 levels
    21. 21. • Returns a value from a list based on a specified index.• If the specified index is not in the list NULL is returned.• Returns the data type based on data type precedence.
    22. 22. • Returns last date of a specified month.• Can specify a month_to_add argument to increment or decrement result.
    23. 23. • DATEFROMPARTS ( year, month, day)• DATETIME2FROMPARTS ( year, month, day, hour, minute, seconds, fractions, precision )• DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds )• DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision )• SMALLDATETIMEFROMPARTS ( year, month, day, hour, minute )• TIMEFROMPARTS ( hour, minute, seconds, fractions, precision )
    24. 24. • Does what it says…concatenates strings together.• NULLs are automatically converted to empty strings.• Can pass other data types for concatenation.
    25. 25. • Simplifies the string formatting of dates and other data types.• No more memorizing numeric predefined format values.• Returns a string.• Uses .Net CLR
    26. 26. • Reduces the need to use RAISERROR in TRY/CATCH blocks.• Can provide custom error messages.• Always uses severity level 16.
    27. 27. Richie Rump@Jorrissjorriss@gmail.comhttp://jorriss.net
    28. 28. Denali T-SQL at a Glance - New and Enhanced Functions Improvements in SQL Server 2012 SERVER – Denali – 14 New Functions – A Quick Guide Test of New Date Manipulation Functions (SQL Spackle) Shortcuts in Visual Studio 2012. Need to import settings.
    29. 29. • MSDN: us/library/ff878091.aspx• Sequence: Why they aren’t just for surrogate keys: /new-in-sql-server-2012-sequences-why-they-arent- just-for-surrogate-keys/• Sequence in 2012: http://www.sql-server-• Create Sequence: t-sql-features-in-sql-server-2012-create-sequence/
    30. 30. • MSDN: us/library/ff878602.aspx• /sql-server-sp_describe_first_result_set- new-system-stored-procedure-in-sql- server-2012/
    31. 31. • MSDN: us/library/ms188332.aspx• WITH RESULT SETS features-in-sql-server-2012-with-result- sets/
    32. 32. • MSDN: us/library/ms188385.aspx• TSQL 2012 – OFFSET and FETCH: offset-and-fetch/• OFFSET and FETCH: features-in-sql-server-2012-offset-and-fetch/
    33. 33. • LAG - MSDN: us/library/hh231256• LEAD - MSDN: us/library/hh213125
    34. 34. • FIRST_VALUE - MSDN: us/library/hh213018• LAST_VALUE - MSDN: us/library/hh231517
    35. 35. • MSDN: us/library/hh213573.aspx• Introduction to PERCENT_RANK(): server-introduction-to-percent_rank-analytic- functions-introduced-in-sql-server-2012/
    36. 36. • MSDN: us/library/hh231078.aspx• Introduction to CUME_DIST: server-introduction-to-cume_dist-analytic- functions-introduced-in-sql-server-2012/
    37. 37. • PERCENTILE_CONT - MSDN: us/library/hh231473.aspx• Introduction to PERCENTILE_CONT: server-introduction-to-percentile_cont-analytic- functions-introduced-in-sql-server-2012/• What exactly does PERCENTILE_CONT do? at-exactly-does-PERCENTILE_CONT-do- anyhow.aspx
    38. 38. • PERCENTILE_DISC - MSDN: us/library/hh231327• Introduction to PERCENTILE_DISC: server-introduction-to-percentile_disc- analytic-functions-introduced-in-sql-server- 2012/
    39. 39. • Difference between PARSE(), TRY_PARSE(), TRY_CONVERT(): denali-conversion-function-difference-between-parse- try_parse-try_convert/• PARSE - MSDN: us/library/hh213316.aspx• TRY_PARSE - MSDN: us/library/hh974669• TRY_CONVERT - MSDN:
    40. 40. • MSDN: us/library/hh213574.aspx
    41. 41. • MSDN: us/library/hh213019• CHOOSE() – A Quick Introduction: server-denali-logical-function-choose-a-quick- introduction/
    42. 42. • MSDN: us/library/hh213020.aspx• EOMONTH() – A Quick Introduction: server-denali-date-and-time-functions- eomonth-a-quick-introduction/
    43. 43. • A Quick Introduction: server-denali-date-and-time-functions- datefromparts-datetimefromparts- datetime2fromparts-timefromparts- smalldatetimefromparts- datetimeoffsetfromparts-a-quick-introduc/
    44. 44. • MSDN: us/library/hh231515.aspx
    45. 45. • MSDN: us/library/hh213505.aspx• New SQL Server Function to Format Dates: w-sql-server-function-to-format- dates/?utm_source=dailynewsletter&utm_mediu m=email&utm_content=headline&utm_campaig n=2012427• Custom Numeric Formats - US/library/0c899ak8.aspx#SpecifierD
    46. 46. • features-in-sql-server-2012-throw/