T-SQL Improvements and Data Types
T-SQL Improvements and Data Types
Module Overview
• Table-Valued Parameters
• T-SQL Assign and Increment Operators
• Row Constructors
• Grouping Sets
• MERGE statement
• Dependency Views
• Performance Enhancements
Table-Valued Parameters
• Inserts into structures with 1-N cardinality problematic
− One order -> N order line items
− "N" is variable and can be large
− Don't want to force a new order for every 20 line items
demo
Table Variables are Input Only
• Declare and initialize TABLE variable
DECLARE @t mytab;
INSERT @t VALUES (1), (2), (3);
EXEC myproc @t;
4000
3500
3000
Parameter List
2500
Table-Valued
Time
2000
Parameter
1500
Bulk Insert
1000
500
0
100 200 400 800 1000 2000 4000 8000 10000
Parameters
T-SQL Syntax Enhancements
• Single statement declare and initialize
DECLARE @i int = 4;
• Row constructors
DECLARE @t TABLE (id int, name varchar(20));
INSERT INTO @t VALUES
(1, 'Fred'), (2, 'Jim'), (3, 'Sue');
Row Constructors
demo
Grouping Sets
• Grouping Sets allow multiple GROUP BY clauses in a single
SQL statement
− Multiple, arbitrary, sets of subtotals
− Single read pass for performance
− Nested subtotals provide ever better performance
demo
GROUPING SETS, ROLLUP, CUBE
• SQL Server 2008 - ANSI-syntax ROLLUP and CUBE
− Pre-2008 non-ANSI syntax is deprecated
demo
More on MERGE
• MERGE statement can reference a $action column
− Used when MERGE used with OUTPUT clause
• MERGE is deterministic
− If more than one row in source matches ON clause, its an error
MERGE Determinism
demo
Keeping Track of Dependencies
• New dependency views replace sp_depends
− Views are kept in sync as changes occur
• sys.dm_sql_referenced_entities
− Lists all named entities that an object references
− Example: which objects does this stored procedure use?
• sys.dm_sql_referencing_entities
− Lists all named entities that use an object
− Example: which objects use this table?
• Can see references at OBJECT, DATABASE DDL TRIGGER,
SERVER DDL TRIGGER level
• sys.sql_expression_dependencies replaces
sys.sql_dependencies at database level
Performance Enhancements
• MERGE and GROUPING SETS offer improvements
− Less scans through table