This document provides information about a conference session on writing queries in SQL Server 2016. The session will cover new statements and clauses in SQL Server 2016-2017 including DROP IF EXISTS, CREATE OR ALTER, TRIM, IIF, STRING_SPLIT, STRING_AGG, CONCAT_WS, TRANSLATE, and sp_execute_external_script. It will also cover querying JSON files and temporal tables. The speaker is Andrea Martorana Tusa from Widex A/S Danmark and has experience speaking at various SQL community events.
3. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• First name: Andrea. Last name: Martorana Tusa.
• Italian, working by Widex a danish company which manufactures
hearing aids, as BI Specialist. Previously worked for 15 years as BI
developer in an italian bank. Focused on database development,
datawarehousing, cube development, reporting, data analysis, etc.
• Speaker at SQL Saturdays, and other community-driven events in
Europe, (MS Cloud Summit, SQL Konferenz, SQL Nexus, SQL Days,
Dataminds Connect ...). Speaker in webinars for PASS Italian VC, DW/BI
VC.
• Author for sqlservercentral.com, sqlshack.com, UGISS (User Group
Italiano SQL Server).
Speaker info
4. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• New statements and clause in SQL Server 2016-17
• Querying temporal tables
• JSON files
Agenda
5. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• No more select from system tables. Now Drop If Exists
(aka DIE) any object.
• Statement applies to following objects: AGGREGATE,
ASSEMBLY, VIEW, DATABASE, DEFAULT, FUNCTION,
INDEX, PROCEDURE, TABLE, ROLE, RULE, SCHEMA,
SECURITY POLICY, SEQUENCE, SYNONYM, TRIGGER,
TYPE, USER
DROP IF EXISTS
6. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Combine CREATE and ALTER statements and create
object if it not exists or alter if it is already created.
• Statement applies to following objects: VIEWS,
STORED PROCEDURES, FUNCTIONS, TRIGGERS.
CREATE OR ALTER
7. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Removes space character or other specified characters
from the start or the end of a string.
• To be used replacing RTRIM/LTRIM
• NB: TRIM is available only from SQL Server 2017.
TRIM
TRIM ([characters FROM] string)
8. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Standard binary IIF function. Condition/condition
TRUE/condition FALSE.
• To be used for simple cases to replace CASE
WHEN statement.
• NB: IIF is available since SQL Server 2012.
IIF
9. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
String split solution is a common request in all forums and support sites. You can find
a bunch of documentation on-line.
Suppose you receive a string in this format:
STRING_SPLIT
Danmark København;Odense;Roskilde;Aaruhs
And want to translate the columns into rows with a function:
Danmark København
Danmark Odense
Danmark Roskilde
Danmark Aarhus
You can do it with a SQL user-defined function. And now in SQL Server 2016 there’s the
function STRING_SPLIT. You don’t need to built your own UDF anymore!
10. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• STRING_AGG is the opposite to STRING_SPLIT. The
function concatenates the values passed from a query
and places separator between them. The separator is
not added at the end of the string
• NB: STRING_AGG is available only from SQL Server
2017.
STRING_AGG
11. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Concatenate With Separator; returns a string resulting
from the concatenation, of two or more string values.
It separates those concatenated string values with the
delimiter specified in the first function argument
• NB: CONCAT_WS is available only from SQL Server
2017.
CONCAT_WS
CONCAT_WS ( separator, argument1, argument1 [,
argumentN]… )
12. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Escapes special characters in texts and returns
text with escaped characters. Used for example
to format JSON files.
STRING_ESCAPE
STRING_ESCAPE( text , type )
13. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Performs a transformation over the input string,
replacing a set of characters with others passed as
argument.
• NB: TRANSLATE is available only from SQL Server 2017.
TRANSLATE
TRANSLATE (inputString, characters, translations)
14. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• New system stored procedure available from SQL Server 2016.
• Used to embed into SQL Server external programming
languages. First release in 2016 supported R, in 2017 support
includes Python, too.
sp_execute_external_script
sp_execute_external_script
@language = N'language',
@script = N'script'
[ , @input_data_1 = N'input_data_1' ]
[ , @input_data_1_name = N'input_data_1_name' ]
[ , @output_data_1_name = N'output_data_1_name' ]
[ , @parallel = 0 | 1 ]
[ , @params = N'@parameter_name data_type [ OUT | OUTPUT ] [ ,...n ]' ]
[ , @parameter1 = 'value1' [ OUT | OUTPUT ] [ ,...n ] ]
R/Python
Script written in the selected language
Input dataset
Output dataset
15. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• SQL Server 2016 introduced native support for JSON to
combine relational and NoSQL concepts. You can easily
transform relational to semi-structured data and vice-versa.
• JSON documents can be processed using standard T-SQL
features. You can therefore store them in tables, set
relationships between them, and query both scalar and JSON
values in one or more tables by using full Transact-SQL.
• As JSON is a text format, it is stored in varchar or nvarchar
columns and is indexed as plain text. There’s no JSON data
type, as in the case of XML.
JSON
16. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Some functions are available to import, query,
process JSON files in SQL Server:
– ISJSON
– JSON_VALUE
– JSON_QUERY
– JSON_MODIFY
– OPENJSON
– FOR JSON
JSON
17. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
• Temporal tables, also called
system-versioned tables, allow
SQL Server to automatically
keep history of the data in the
table.
• Temporal tables keep the
history of the data, tracking all
changes with a start and end
date, to mark when the record
was active.
Temporal tables
The current table contains the value for each row
The history table contains each previous value
for each row, if any, and the start time and end
time for the period for which it was valid
18. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
Temporal tables
CREATE TABLE Department
(
DeptID int NOT NULL PRIMARY KEY CLUSTERED
, DeptName varchar(50) NOT NULL
, ManagerID INT NULL
, ParentDeptID int NULL
, SysStartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL
, SysEndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL
, PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
)
WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.DepartmentHistory)
)
;
Start and end date always in the format datetime2
Temporal table
outlines
Primary key definition mandatory
Definition for destination table:
a) Hidden
b) Default
c) User-defined
19. @ITCAMPRO #ITCAMP18Community Conference for IT Professionals
How to query temporal tables
New clause FOR SYSTEM
TIME in the SELECT
statement, with five sub-
clauses to query data
across the current and
history tables.
SELECT * FROM Employee
FOR SYSTEM_TIME
BETWEEN '2014-01-01 00:00:00.0000000' AND '2015-01-01 00:00:00.0000000'
WHERE EmployeeID = 1000 ORDER BY ValidFrom;