TeamStation AI System Report LATAM IT Salaries 2024
ITCamp 2013 - Cristian Lefter - Transact-SQL from 0 to SQL Server 2012
1. itcampro@ itcamp13# Premium conference on Microsoft technologies
Transact-SQL from 0 to SQL
Server 2012
Cristian Lefter
SQL Server MVP
http://about.me/CristianLefter
2. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesHuge thanks to our sponsors!
3. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Introduction
• SQL Server 20XX
• Next Steps
Agenda
5. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Initially developed at IBM by Donald D. Chamberlin and
Raymond F. Boyce in the early 1970s under the name of
SEQUEL (Structured English Query Language)
• Designed to manipulate and retrieve data stored in IBM's
original quasi-relational database management system, System
R
• Based on Edgar F. Codd's relational model, described in his
1970 paper "A Relational Model of Data for Large Shared Data
Banks”
• Was renamed SQL because "SEQUEL" was a trademark of an
UK-based aircraft company.
• SQL becomes a standard of the American National Standards
Institute (ANSI) in 1986 and for International Organization for
Standards (ISO) in 1987
SQL - history
6. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Version Year Release Name Codename
1.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 SQLNT
6.0 1995 SQL Server 6.0 SQL95
6.5 1996 SQL Server 6.5 Hydra
7.0 1998 SQL Server 7.0 Sphinx
- 1999 SQL Server 7.0
OLAP Tools
Plato mania
Microsoft SQL Server History
7. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Version Year Release Name Codename
8.0 2000 SQL Server 2000 Shiloh
8.0 2003 SQL Server 2000
64-bit Edition
Liberty
9.0 2005 SQL Server 2005 Yukon
10.0 2008 SQL Server 2008 Katmai
10.25 2010 SQL Azure DB CloudDatabase
10.5 2010 SQL Server 2008
R2
Kilimanjaro (aka KJ)
11.0 2012 SQL Server 2012 Denali
Microsoft SQL Server History (cont.)
9. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesVersion 1.1 on OS/2 – SQL Admin Facility
10. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
What is a database?
A collection of objects known as tables. For the
OS, a database is simply a file.
What is a table?
A table is a set of data elements (values) that is
organized using a model of vertical columns
(which are identified by their name) and
horizontal rows.
Database related terms
11. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Are these familiar to you?
– SELECT / INSERT / UPDATE / DELETE
• How about these?
– GROUP BY, PROCEDURE, FUNCTION, TRIGGER
Where Do We Start From?
13. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• XML support - FOR XML clause
• User-Defined Functions
• Indexed Views
• INSTEAD OF and AFTER triggers
New in SQL Server 2000
15. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Syntax:
Common Table Expressions (CTE)
WITH <common_table_expression> [ ,...n ]
<common_table_expression>::=
expression_name [ ( column_name [
,...n ] )]
AS ( CTE_query_definition )
16. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Syntax:
OUTPUT clause
OUTPUT <dml_select_list> INTO {
@table_variable | output_table } [ (
column_list ) ]
[ OUTPUT <dml_select_list> ]
17. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• CROSS APPLY
• OUTER APPLY
Syntax:
APPLY operator
FROM left_table_source
{OUTER | CROSS} APPLY right_table_source
18. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Partial Syntax:
PIVOT and UNPIVOT operators
SELECT * FROM table_source
PIVOT (aggregate_function ( value_column )
FOR pivot_column IN ( <column_list> )
) table_alias
19. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• CREATE LOGIN,
• ALTER LOGIN
• DROP LOGIN
• CREATE USER
• ALTER USER
• DROP USER
Security Statements
20. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Syntax:
Error Handling
BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
{ sql_statement | statement_block }
END CATCH
[ ; ]
21. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• RANK
• DENSE_RANK
• NTILE
• ROW_NUMBER
Ranking Functions
22. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• .NET Integration
• XML support (xml data type, XQUERY
support, XML Data Manipulation
Language, XML Schemas support)
• VARCHAR(MAX), VARBINARY(MAX)
• Execution context – EXECUTE AS
• Schemas
Other things to mention
23. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
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.)
25. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Row Constructors
• One statement variable
declaration/assignment
• Compound assignment operators
T-SQL delighters
26. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Syntax:
INSERT over DML
OUTPUT <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. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Example of usage:
Table-valued parameters
CREATE TABLE StockTransactions(TransactionID INT PRIMARY KEY
IDENTITY(1,1),StockSymbol VARCHAR(64),Qty INT);
GO
CREATE TYPE TransactionType AS TABLE (StockSymbol VARCHAR(64),
Qty INT);
GO
CREATE PROCEDURE usp_InsertTransaction
@TVP TransactionType READONLY
AS
BEGIN
SET NOCOUNT ON
INSERT INTO StockTransactions(StockSymbol,Qty)
SELECT StockSymbol,Qty FROM @TVP;
END;
GO
28. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Partial syntax:
MERGE statement
MERGE
[INTO] target_table
USING <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. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• DATE
• TIME
• DATETIME2
• DATETIMEOFFSET
Date and Time Data Types
30. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
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 Compression
Other things to mention
32. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Nothing major for T-SQL except UNICODE
compression
SQL Server 2008 R2
34. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best PracticesQuery Pagination
Syntax:
Example:
OFFSET <offset_value> ROW|ROWS
FETCH FIRST|NEXT <fetch_value> ROW|ROWS
[ONLY]
SELECT ProductID, Name,Color, Size
FROM Production.Product
ORDER BY Color,ProductID ASC
OFFSET 100 ROWS
FETCH NEXT 25 ROWS ONLY;
END;
35. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Syntax:
Sequences
CREATE 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. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• SQL:2008 Compliant
• Partial Syntax:
• New analytic functions
Extended support for Window Functions
OVER (
[ <PARTITION BY clause> ]
[ <ORDER BY clause> ]
[ <ROW or RANGE clause> ]
)
CUME_DIST LEAD FIRST_VALUE
PERCENTILE_CONT LAG PERCENTILE_DISC
LAST_VALUE PERCENT_RANK
37. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Example
Extended support for Window Functions
(cont.)
SELECT
PS.Name AS SubcategoryName,
P.Name AS ProductName,
P.ListPrice,
FIRST_VALUE(P.Name) OVER (PARTITION BY
P.ProductSubcategoryID
ORDER BY P.ListPrice ASC) AS LeastExpensive
FROM Production.Product P
JOIN Production.ProductSubcategory PS
ON P.ProductSubcategoryID = PS.ProductSubcategoryID
38. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Conversion functions
– PARSE
– TRY_CONVERT
– TRY_PARSE
• Date and time functions
– DATEFROMPARTS
– DATETIME2FROMPARTS
– DATETIMEFROMPARTS
– DATETIMEOFFSETFROMPARTS
– EOMONTH
– SMALLDATETIMEFROMPARTS
– TIMEFROMPARTS
New Functions
39. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Logical functions
– CHOOSE
– IIF
• String functions
– CONCAT
– FORMAT
New Functions (cont.)
40. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
Syntax
THROW
THROW [ { error_number |
@local_variable },
{ message | @local_variable
},
{ state | @local_variable } ]
[ ; ]
42. itcampro@ itcamp13# Premium conference on Microsoft technologies
Architecture &
Best Practices
• Read about Hekaton
• SQL Server 2012 Update for Developers
Training Workshop http://bit.ly/sql2012ro
• ROSQL http://sqlserver.ro
Next Steps