SlideShare a Scribd company logo
Presented by Steve Stedman and Aaron Buma
Presented by Steve Stedman and Aaron Buma
Presented by Steve Stedman and Aaron Buma
Welcome
 Welcome to all those joining us remotely.
 For any questions via Google On Air Broadcasts, we will
address most of these at the end of the training.
 Training provided by Emergency Reporting
 http://EmergencyReporting.com
Live Broadcast
 Using Google On Air Broadcasts
 There is about a 40 to 50 second delay from live to what
you see.
 We are still learning how to properly use Google On Air
Broadcasts. Please be patient.
 Session will be available on my YouTube Channel about
an hour after it the presentation ends.
 http://SteveStedman.com/YouTube
Questions
 We will have time for questions at the end of the session.
 Q&A available via Google On Air Hangout panel. Click the
3x3 grid icon near the top right, then select Q&A to see
what people are asking, or to ask your own question.
 When you ask a question, it shows up for us about 40 to 50
seconds delayed.
Agenda
 Correlated Subqueries and Subquery Extensions
 Correlated Sub Queries
 Sub Query Extensions (ANY, ALL, SOME)
 Exists
 OUTPUT Clause
Presented by Aaron Buma
Correlating Subqueries Together
 Also known as derived tables, a correlated subquery is
a nested query
 It can be in the selected columns, in a join or as a filter
in the where clause
 Subqueries return result sets to their outer query
 You can nest up to 32 queries together
SubQuery Extensions
 Used in the where clause to filter on the query based
on a single-column correlated query
 Operators: ALL, ANY, SOME, EXISTS
ALL
 Syntax: ‘WHERE 5 <= ALL (SELECT col1 FROM..)
 All rows of col1 must match the condition ( col1 <= 5 )
in order for that correlated condition to be true
ANY
 Syntax: ‘WHERE 5 <= ANY (SELECT col1 FROM..)
 Any of the rows of col1 must match the condition ( col1
<= 5 ) in order for that correlated condition to be true
SOME
 Syntax: ‘WHERE 5 <= SOME (SELECT col1 FROM..)
 Is the ISO standard of ANY, they will return the same
results
EXISTS
 Syntax: ‘WHERE EXISTS (SELECT column FROM..)
 A hardcoded value (1,’x’) can be used instead of a
column because the columns data is not actually
returned
 The first match resolves the condition as TRUE and
the parser stops evaluating for the correlated value
 Is more performant than ANY or SOME because it
doesn’t have to process the all of the correlated results
Basic Subqueries and
Correllated Subqueries
Demo
Subquery Extensions:
ALL, ANY, SOME and EXISTS
Demo
Review
 Correlated Query – Filtering a derived table or subquery
based on a column in the main query.
 Using the ‘ALL’ operator returns TRUE only if all rows fit
the condition
 Using ‘ANY’ or ‘SOME’ returns TRUE if one or more of the
subquery rows match condition
 Using ‘EXISTS’ has the same results as ‘ANY’ or ‘SOME’
does not return data and will stop processing at first match
with the condition, returning TRUE
This training is sponsored by Emergency Reporting.
Visit http://EmergencyReporting.com for more details.
Presented by Steve Stedman
What is the OUTPUT clause
 A way to get a result set on queries that don’t normally
return a result set.
 For instance INSERT, DELETE, UPDATE, and MERGE.
OUTPUT Clause Applies To
 INSERT
 DELETE
 UPDATE
 MERGE
 Using output with MERGE will be covered when we
cover the MERGE Statement
Availability of the OUTPUT Clause
 SQL Server 2005 and newer
 Available on all editions
 Express
 Standard
 Enterprise
 Developer
 Azure
INSERT
OUTPUT INSERTED.*
 Displays the entire row that was just inserted.
Returning the IDENTITY inserted
without the OUTPUT clause
 @@IDENTITY returns the last identity value generated for any table in
the current session, across all scopes. You need to be careful here,
since it's across scopes. You could get a value from a trigger, instead of
your current statement.
 SCOPE_IDENTITY returns the last identity value generated for any
table in the current session and the current scope. Generally what you
want to use.
 IDENT_CURRENT returns the last identity value generated for a
specific table in any session and any scope. This lets you specify which
table you want the value from, in case the two above aren't quite what
you need (very rare). You could use this if you want to get the current
IDENTITY value for a table that you have not inserted a record into.
IDENTITY values of
multi-row INSERT
 These only return one:
 @@IDENTITY
 SCOPE_IDENTITY
 IDENT_CURRENT
 OUTPUT INSERTED.*
 Will return all inserted.
OUTPUT Clause – With an INSERT
Statement
Demo
UPDATE
[DELETED].*, [INSERTED].*
 DELETED returns the before value.
 INSERTED returns the after value.
OUTPUT Clause – With an UPDATE
Statement
Demo
DELETE
[DELETED].*
 DELETED returns the row that was deleted.
OUTPUT Clause – With an DELETE
Statement
Demo
Auditing with the OUTPUT Clause
Demo
OUTPUT Clause Gotchas
 Get results, even if the transaction fails.
 Columns returned from OUTPUT reflect the data as it
exists after the INSERT, UPDATE, or DELETE
statement has completed but before triggers are
executed.
Not supported in the following:
 DML statements that reference local partitioned
views, distributed partitioned views, or remote tables.
 INSERT statements that contain an EXECUTE
statement.
 A user-defined function cannot be created if it
contains an OUTPUT INTO clause that has a table as
its target.
Review
 INSERT
 DELETE
 UPDATE
 MERGE
 Using output with MERGE will be covered when we
cover the MERGE Statement
Quiz 1:
 All of the following are true, so which is better to use:
ANY, SOME or EXISTS?
a) ANY because it comes first alphabetically
b) SOME because it is the ISO standard of ANY
c) EXISTS because it is more performant than ANY or
SOME
Quiz 2:
 Which is better to track or log changes to a table, the
OUTPUT clause or a TRIGGER?
a) The OUTPUT clause has less overhead, therefor it is better.
b) A TRIGGER is the best option since it guarantees the
information will be logged.
c) It depends on the specific environment, and your specific
needs. In some cases the OUTPUT clause is better, and in
other cases the TRIGGER is the best option.
Any Questions?
 OUTPUT Clause
 Derived Table Queries (ie Subqueries)
 Correlated Sub Queries
 Sub Query Extensions (ANY, ALL, SOME)
 Exists
For More Information
 Visit http://EmergencyReporting.com to find out more
about Emergency Reporting.
 Aaron on the web
 http://AaronBuma.com
 Twitter: @AaronDBuma
 Steve on the web
 http://SteveStedman.com
 twitter: @SqlEmt
Tune in next week
 Thursday 2/17 at 9:00am (pacific time).
 Topics
 Data Recursion
 Self JOIN
 Range Heirarchy
 Recursive CTE
 Merge Statement

More Related Content

Similar to OUTPUT Clause Correlated Subqueries and Subquery Extensions

Set operators - derived tables and CTEs
Set operators - derived tables and CTEsSet operators - derived tables and CTEs
Set operators - derived tables and CTEs
Steve Stedman
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
prabhu rajendran
 
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdfOracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
SkillCertProExams
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Sql General
Sql General Sql General
Sql General
Praveen Tiwari
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
osama majid
 
Sql basics
Sql basicsSql basics
Sql basics
Kumar
 
control statements
control statementscontrol statements
control statements
Azeem Sultan
 
Data integrity
Data integrityData integrity
Data integrity
Rahul Gupta
 
5 black box and grey box testing
5   black box and grey box testing5   black box and grey box testing
5 black box and grey box testing
Yisal Khan
 
Stored procedures
Stored proceduresStored procedures
Fitnesse Testing Framework
Fitnesse Testing Framework Fitnesse Testing Framework
Fitnesse Testing Framework
Ajit Koti
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's New
dpcobb
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
mcollison
 
Sql Server 2008 New Programmability Features
Sql Server 2008 New Programmability FeaturesSql Server 2008 New Programmability Features
Sql Server 2008 New Programmability Features
sqlserver.co.il
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
Vladislav Hadzhiyski
 
Black-Box.ppt
Black-Box.pptBlack-Box.ppt
Black-Box.ppt
coctheweeknd
 
Calamities with cardinalities
Calamities with cardinalitiesCalamities with cardinalities
Calamities with cardinalities
Randolf Geist
 

Similar to OUTPUT Clause Correlated Subqueries and Subquery Extensions (20)

Set operators - derived tables and CTEs
Set operators - derived tables and CTEsSet operators - derived tables and CTEs
Set operators - derived tables and CTEs
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdfOracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
Sql General
Sql General Sql General
Sql General
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql basics
Sql basicsSql basics
Sql basics
 
control statements
control statementscontrol statements
control statements
 
Data integrity
Data integrityData integrity
Data integrity
 
5 black box and grey box testing
5   black box and grey box testing5   black box and grey box testing
5 black box and grey box testing
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Fitnesse Testing Framework
Fitnesse Testing Framework Fitnesse Testing Framework
Fitnesse Testing Framework
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's New
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Sql Server 2008 New Programmability Features
Sql Server 2008 New Programmability FeaturesSql Server 2008 New Programmability Features
Sql Server 2008 New Programmability Features
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
 
Black-Box.ppt
Black-Box.pptBlack-Box.ppt
Black-Box.ppt
 
Calamities with cardinalities
Calamities with cardinalitiesCalamities with cardinalities
Calamities with cardinalities
 

Recently uploaded

System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
marufrahmanstratejm
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 

Recently uploaded (20)

System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 

OUTPUT Clause Correlated Subqueries and Subquery Extensions

  • 1. Presented by Steve Stedman and Aaron Buma
  • 2. Presented by Steve Stedman and Aaron Buma
  • 3. Presented by Steve Stedman and Aaron Buma
  • 4. Welcome  Welcome to all those joining us remotely.  For any questions via Google On Air Broadcasts, we will address most of these at the end of the training.  Training provided by Emergency Reporting  http://EmergencyReporting.com
  • 5. Live Broadcast  Using Google On Air Broadcasts  There is about a 40 to 50 second delay from live to what you see.  We are still learning how to properly use Google On Air Broadcasts. Please be patient.  Session will be available on my YouTube Channel about an hour after it the presentation ends.  http://SteveStedman.com/YouTube
  • 6. Questions  We will have time for questions at the end of the session.  Q&A available via Google On Air Hangout panel. Click the 3x3 grid icon near the top right, then select Q&A to see what people are asking, or to ask your own question.  When you ask a question, it shows up for us about 40 to 50 seconds delayed.
  • 7. Agenda  Correlated Subqueries and Subquery Extensions  Correlated Sub Queries  Sub Query Extensions (ANY, ALL, SOME)  Exists  OUTPUT Clause
  • 9. Correlating Subqueries Together  Also known as derived tables, a correlated subquery is a nested query  It can be in the selected columns, in a join or as a filter in the where clause  Subqueries return result sets to their outer query  You can nest up to 32 queries together
  • 10. SubQuery Extensions  Used in the where clause to filter on the query based on a single-column correlated query  Operators: ALL, ANY, SOME, EXISTS
  • 11. ALL  Syntax: ‘WHERE 5 <= ALL (SELECT col1 FROM..)  All rows of col1 must match the condition ( col1 <= 5 ) in order for that correlated condition to be true
  • 12. ANY  Syntax: ‘WHERE 5 <= ANY (SELECT col1 FROM..)  Any of the rows of col1 must match the condition ( col1 <= 5 ) in order for that correlated condition to be true
  • 13. SOME  Syntax: ‘WHERE 5 <= SOME (SELECT col1 FROM..)  Is the ISO standard of ANY, they will return the same results
  • 14. EXISTS  Syntax: ‘WHERE EXISTS (SELECT column FROM..)  A hardcoded value (1,’x’) can be used instead of a column because the columns data is not actually returned  The first match resolves the condition as TRUE and the parser stops evaluating for the correlated value  Is more performant than ANY or SOME because it doesn’t have to process the all of the correlated results
  • 16. Subquery Extensions: ALL, ANY, SOME and EXISTS Demo
  • 17. Review  Correlated Query – Filtering a derived table or subquery based on a column in the main query.  Using the ‘ALL’ operator returns TRUE only if all rows fit the condition  Using ‘ANY’ or ‘SOME’ returns TRUE if one or more of the subquery rows match condition  Using ‘EXISTS’ has the same results as ‘ANY’ or ‘SOME’ does not return data and will stop processing at first match with the condition, returning TRUE
  • 18. This training is sponsored by Emergency Reporting. Visit http://EmergencyReporting.com for more details.
  • 20. What is the OUTPUT clause  A way to get a result set on queries that don’t normally return a result set.  For instance INSERT, DELETE, UPDATE, and MERGE.
  • 21. OUTPUT Clause Applies To  INSERT  DELETE  UPDATE  MERGE  Using output with MERGE will be covered when we cover the MERGE Statement
  • 22. Availability of the OUTPUT Clause  SQL Server 2005 and newer  Available on all editions  Express  Standard  Enterprise  Developer  Azure
  • 23. INSERT OUTPUT INSERTED.*  Displays the entire row that was just inserted.
  • 24. Returning the IDENTITY inserted without the OUTPUT clause  @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes. You need to be careful here, since it's across scopes. You could get a value from a trigger, instead of your current statement.  SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope. Generally what you want to use.  IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope. This lets you specify which table you want the value from, in case the two above aren't quite what you need (very rare). You could use this if you want to get the current IDENTITY value for a table that you have not inserted a record into.
  • 25. IDENTITY values of multi-row INSERT  These only return one:  @@IDENTITY  SCOPE_IDENTITY  IDENT_CURRENT  OUTPUT INSERTED.*  Will return all inserted.
  • 26. OUTPUT Clause – With an INSERT Statement Demo
  • 27. UPDATE [DELETED].*, [INSERTED].*  DELETED returns the before value.  INSERTED returns the after value.
  • 28. OUTPUT Clause – With an UPDATE Statement Demo
  • 29. DELETE [DELETED].*  DELETED returns the row that was deleted.
  • 30. OUTPUT Clause – With an DELETE Statement Demo
  • 31. Auditing with the OUTPUT Clause Demo
  • 32. OUTPUT Clause Gotchas  Get results, even if the transaction fails.  Columns returned from OUTPUT reflect the data as it exists after the INSERT, UPDATE, or DELETE statement has completed but before triggers are executed.
  • 33. Not supported in the following:  DML statements that reference local partitioned views, distributed partitioned views, or remote tables.  INSERT statements that contain an EXECUTE statement.  A user-defined function cannot be created if it contains an OUTPUT INTO clause that has a table as its target.
  • 34. Review  INSERT  DELETE  UPDATE  MERGE  Using output with MERGE will be covered when we cover the MERGE Statement
  • 35. Quiz 1:  All of the following are true, so which is better to use: ANY, SOME or EXISTS? a) ANY because it comes first alphabetically b) SOME because it is the ISO standard of ANY c) EXISTS because it is more performant than ANY or SOME
  • 36. Quiz 2:  Which is better to track or log changes to a table, the OUTPUT clause or a TRIGGER? a) The OUTPUT clause has less overhead, therefor it is better. b) A TRIGGER is the best option since it guarantees the information will be logged. c) It depends on the specific environment, and your specific needs. In some cases the OUTPUT clause is better, and in other cases the TRIGGER is the best option.
  • 37. Any Questions?  OUTPUT Clause  Derived Table Queries (ie Subqueries)  Correlated Sub Queries  Sub Query Extensions (ANY, ALL, SOME)  Exists
  • 38. For More Information  Visit http://EmergencyReporting.com to find out more about Emergency Reporting.  Aaron on the web  http://AaronBuma.com  Twitter: @AaronDBuma  Steve on the web  http://SteveStedman.com  twitter: @SqlEmt
  • 39. Tune in next week  Thursday 2/17 at 9:00am (pacific time).  Topics  Data Recursion  Self JOIN  Range Heirarchy  Recursive CTE  Merge Statement