Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
itcampro@ itcamp13# Premium conference on Microsoft technologiesTransact-SQL from 0 to SQLServer 2012Cristian LefterSQL Se...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesHuge thanks to our sponsors!
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Introduction• SQL Server 20X...
itcampro@ itcamp13# Premium conference on Microsoft technologiesINSTEAD OF ANINTRODUCTIONSection 1
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Initially developed at IBM b...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion Year Release Name Code...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion Year Release Name Code...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion 1.0
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion 1.1 on OS/2 – SQL Admi...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesWhat is a database?A collectio...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Are these familiar to you?– ...
itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2000Section 2
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• XML support - FOR XML clause...
itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2005Section 2
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:Common Table Expression...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:OUTPUT clauseOUTPUT <dm...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• CROSS APPLY• OUTER APPLYSynt...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesPartial Syntax:PIVOT and UNPIV...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• CREATE LOGIN,• ALTER LOGIN• ...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:Error HandlingBEGIN TRY...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• RANK• DENSE_RANK• NTILE• ROW...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• .NET Integration• XML suppor...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Full-text Search DDL– CREATE...
itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2008Section 3
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Row Constructors• One statem...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:INSERT over DMLOUTPUT <...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesExample of usage:Table-valued ...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesPartial syntax:MERGE statement...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• DATE• TIME• DATETIME2• DATET...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Sparse Columns and Column Se...
itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2008 R2Section 4
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Nothing major for T-SQL exce...
itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2012Section 4
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesQuery PaginationSyntax:Example...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:SequencesCREATE SEQUENC...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• SQL:2008 Compliant• Partial ...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• ExampleExtended support for ...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Conversion functions– PARSE–...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Logical functions– CHOOSE– I...
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntaxTHROWTHROW [ { error_num...
itcampro@ itcamp13# Premium conference on Microsoft technologiesNEXT STEPSSection 5
itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Read about Hekaton• SQL Serv...
itcampro@ itcamp13# Premium conference on Microsoft technologiesQ & A
itcampro@ itcamp13# Premium conference on Microsoft technologiesTHANK YOU!
Upcoming SlideShare
Loading in …5
×

ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012

686 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012

  1. 1. itcampro@ itcamp13# Premium conference on Microsoft technologiesTransact-SQL from 0 to SQLServer 2012Cristian LefterSQL Server MVPhttp://about.me/CristianLefter
  2. 2. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesHuge thanks to our sponsors!
  3. 3. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Introduction• SQL Server 20XX• Next StepsAgenda
  4. 4. itcampro@ itcamp13# Premium conference on Microsoft technologiesINSTEAD OF ANINTRODUCTIONSection 1
  5. 5. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Initially developed at IBM by Donald D. Chamberlin andRaymond F. Boyce in the early 1970s under the name ofSEQUEL (Structured English Query Language)• Designed to manipulate and retrieve data stored in IBMsoriginal quasi-relational database management system, SystemR• Based on Edgar F. Codds relational model, described in his1970 paper "A Relational Model of Data for Large Shared DataBanks”• Was renamed SQL because "SEQUEL" was a trademark of anUK-based aircraft company.• SQL becomes a standard of the American National StandardsInstitute (ANSI) in 1986 and for International Organization forStandards (ISO) in 1987SQL - history
  6. 6. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion Year Release Name Codename1.0(OS/2)1989 SQL Server 1.0(16 bit)-1.1(OS/2)1991 SQL Server 1.1(16 bit)-4.21(WinNT)1993 SQL Server 4.21 SQLNT6.0 1995 SQL Server 6.0 SQL956.5 1996 SQL Server 6.5 Hydra7.0 1998 SQL Server 7.0 Sphinx- 1999 SQL Server 7.0OLAP ToolsPlato maniaMicrosoft SQL Server History
  7. 7. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion Year Release Name Codename8.0 2000 SQL Server 2000 Shiloh8.0 2003 SQL Server 200064-bit EditionLiberty9.0 2005 SQL Server 2005 Yukon10.0 2008 SQL Server 2008 Katmai10.25 2010 SQL Azure DB CloudDatabase10.5 2010 SQL Server 2008R2Kilimanjaro (aka KJ)11.0 2012 SQL Server 2012 DenaliMicrosoft SQL Server History (cont.)
  8. 8. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion 1.0
  9. 9. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesVersion 1.1 on OS/2 – SQL Admin Facility
  10. 10. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesWhat is a database?A collection of objects known as tables. For theOS, a database is simply a file.What is a table?A table is a set of data elements (values) that isorganized using a model of vertical columns(which are identified by their name) andhorizontal rows.Database related terms
  11. 11. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Are these familiar to you?– SELECT / INSERT / UPDATE / DELETE• How about these?– GROUP BY, PROCEDURE, FUNCTION, TRIGGERWhere Do We Start From?
  12. 12. itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2000Section 2
  13. 13. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• XML support - FOR XML clause• User-Defined Functions• Indexed Views• INSTEAD OF and AFTER triggersNew in SQL Server 2000
  14. 14. itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2005Section 2
  15. 15. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:Common Table Expressions (CTE)WITH <common_table_expression> [ ,...n ]<common_table_expression>::=expression_name [ ( column_name [,...n ] )]AS ( CTE_query_definition )
  16. 16. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:OUTPUT clauseOUTPUT <dml_select_list> INTO {@table_variable | output_table } [ (column_list ) ][ OUTPUT <dml_select_list> ]
  17. 17. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• CROSS APPLY• OUTER APPLYSyntax:APPLY operatorFROM left_table_source{OUTER | CROSS} APPLY right_table_source
  18. 18. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesPartial Syntax:PIVOT and UNPIVOT operatorsSELECT * FROM table_sourcePIVOT (aggregate_function ( value_column )FOR pivot_column IN ( <column_list> )) table_alias
  19. 19. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• CREATE LOGIN,• ALTER LOGIN• DROP LOGIN• CREATE USER• ALTER USER• DROP USERSecurity Statements
  20. 20. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:Error HandlingBEGIN TRY{ sql_statement | statement_block }END TRYBEGIN CATCH{ sql_statement | statement_block }END CATCH[ ; ]
  21. 21. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• RANK• DENSE_RANK• NTILE• ROW_NUMBERRanking Functions
  22. 22. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• .NET Integration• XML support (xml data type, XQUERYsupport, XML Data ManipulationLanguage, XML Schemas support)• VARCHAR(MAX), VARBINARY(MAX)• Execution context – EXECUTE AS• SchemasOther things to mention
  23. 23. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Full-text Search DDL– CREATE FULLTEXT CATALOG– CREATE FULLTEXT INDEX• DDL Triggers• Event Notifications• Service Broker– SEND– RECEIVE• Partitioning– CREATE PARTITION FUNCTION– CREATE PARTITION SCHEME– CREATE TABLE table_name ON partition_scheme_name(column_name)Other things to mention (cont.)
  24. 24. itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2008Section 3
  25. 25. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Row Constructors• One statement variabledeclaration/assignment• Compound assignment operatorsT-SQL delighters
  26. 26. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:INSERT over DMLOUTPUT <dml_select_list> INTO{@table_variable|output_table}[(column_list)]][OUTPUT<dml_select_list>]<dml_select_list> ::={ <column_name> | scalar_expression } [ [AS]column_alias_identifier ][ ,...n ]<column_name> ::={DELETED|INSERTED|from_table_name}.{*|column_name}|$ACTION
  27. 27. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesExample of usage:Table-valued parametersCREATE TABLE StockTransactions(TransactionID INT PRIMARY KEYIDENTITY(1,1),StockSymbol VARCHAR(64),Qty INT);GOCREATE TYPE TransactionType AS TABLE (StockSymbol VARCHAR(64),Qty INT);GOCREATE PROCEDURE usp_InsertTransaction@TVP TransactionType READONLYASBEGINSET NOCOUNT ONINSERT INTO StockTransactions(StockSymbol,Qty)SELECT StockSymbol,Qty FROM @TVP;END;GO
  28. 28. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesPartial syntax:MERGE statementMERGE[INTO] target_tableUSING <table_source>ON <search_condition>[WHEN MATCHED [AND <search_condition>]THEN <merge_matched>][WHEN [TARGET] NOT MATCHED [AND <search_condition>]THEN <merge_not_matched>][WHEN SOURCE NOT MATCHED [AND <search_condition>]THEN <merge_matched>]<output_clause>;
  29. 29. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• DATE• TIME• DATETIME2• DATETIMEOFFSETDate and Time Data Types
  30. 30. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Sparse Columns and Column Sets• HierarchyID data type• GROUP BY Enhancements• Resource Governor• Transparent Data Encryption• SQL Server Audit• Extended Events• Data Compression• Backup CompressionOther things to mention
  31. 31. itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2008 R2Section 4
  32. 32. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Nothing major for T-SQL except UNICODEcompressionSQL Server 2008 R2
  33. 33. itcampro@ itcamp13# Premium conference on Microsoft technologiesSQL SERVER 2012Section 4
  34. 34. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesQuery PaginationSyntax:Example:OFFSET <offset_value> ROW|ROWSFETCH FIRST|NEXT <fetch_value> ROW|ROWS[ONLY]SELECT ProductID, Name,Color, SizeFROM Production.ProductORDER BY Color,ProductID ASCOFFSET 100 ROWSFETCH NEXT 25 ROWS ONLY;END;
  35. 35. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntax:SequencesCREATE SEQUENCE [schema_name . ] sequence_name[ 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 } ][ ; ]]
  36. 36. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• SQL:2008 Compliant• Partial Syntax:• New analytic functionsExtended support for Window FunctionsOVER ([ <PARTITION BY clause> ][ <ORDER BY clause> ][ <ROW or RANGE clause> ])CUME_DIST LEAD FIRST_VALUEPERCENTILE_CONT LAG PERCENTILE_DISCLAST_VALUE PERCENT_RANK
  37. 37. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• ExampleExtended support for Window Functions(cont.)SELECTPS.Name AS SubcategoryName,P.Name AS ProductName,P.ListPrice,FIRST_VALUE(P.Name) OVER (PARTITION BYP.ProductSubcategoryIDORDER BY P.ListPrice ASC) AS LeastExpensiveFROM Production.Product PJOIN Production.ProductSubcategory PSON P.ProductSubcategoryID = PS.ProductSubcategoryID
  38. 38. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Conversion functions– PARSE– TRY_CONVERT– TRY_PARSE• Date and time functions– DATEFROMPARTS– DATETIME2FROMPARTS– DATETIMEFROMPARTS– DATETIMEOFFSETFROMPARTS– EOMONTH– SMALLDATETIMEFROMPARTS– TIMEFROMPARTSNew Functions
  39. 39. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Logical functions– CHOOSE– IIF• String functions– CONCAT– FORMATNew Functions (cont.)
  40. 40. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best PracticesSyntaxTHROWTHROW [ { error_number |@local_variable },{ message | @local_variable},{ state | @local_variable } ][ ; ]
  41. 41. itcampro@ itcamp13# Premium conference on Microsoft technologiesNEXT STEPSSection 5
  42. 42. itcampro@ itcamp13# Premium conference on Microsoft technologiesArchitecture &Best Practices• Read about Hekaton• SQL Server 2012 Update for DevelopersTraining Workshop http://bit.ly/sql2012ro• ROSQL http://sqlserver.roNext Steps
  43. 43. itcampro@ itcamp13# Premium conference on Microsoft technologiesQ & A
  44. 44. itcampro@ itcamp13# Premium conference on Microsoft technologiesTHANK YOU!

×