SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
-
Upload
steve-stedman -
Category
Technology
-
view
594 -
download
1
Transcript of SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
![Page 1: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/1.jpg)
MERGE Statement and Recursive Queries
Presented by Steve Stedman and Aaron Buma
![Page 2: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/2.jpg)
WelcomeWelcome 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 Reportinghttp://EmergencyReporting.com
Slides and sample code are available at:http://SteveStedman.com
![Page 3: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/3.jpg)
Welcome Viewers FromBellingham, WARiverview, FLWarren, MIJacksonville, FL
Chennai, IndiaBelarusTrowbridge, UKHyderabad, IndiaBucharest, Romania
![Page 4: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/4.jpg)
Live BroadcastUsing 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 the presentation ends. http://SteveStedman.com/YouTube
![Page 5: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/5.jpg)
QuestionsWe 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.
![Page 6: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/6.jpg)
A Question From a Viewer“on your webpage you mention the training will be covered in 45 weeks, do you mean to say that the preparation for the 70-461 exam will take almost a year.”
That was the original plan, however we have had several weeks that we have doubled up content from the original plan, so I am thinking it will be around 30 weeks, instead of the original estimated 45.
![Page 7: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/7.jpg)
AgendaMerge StatementData Recursion
Self JOINRecursive CTE
![Page 8: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/8.jpg)
MERGE Statement
Presented by Aaron Buma
![Page 9: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/9.jpg)
Without Merge – Update/InsertWithout merge, conditional
Inserts/Updates/Deletes require multiple statements
Example: Combine your baseball card book with a new deckWithout Merge:
“Insert” into your deck cards you don’t have “Update” the quantity of cards you have if you already
have it and if the value is > $5
![Page 10: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/10.jpg)
Merge - ExampleWith Merge:
![Page 11: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/11.jpg)
Merge StatementWhat it can do in one statement:
Perform 2 or 3 UPDATE, INSERT, DELETE statements 2 Statements
One for a MATCHED One for NOT MATCHED
3 Statements One for MATCHED x = y AND x > z One for MATCHED x = y One for NOT MATCHED
![Page 12: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/12.jpg)
Merge Statement - AdvancedThe source ‘table’ can be a derived table
VariablesSubselect
You can use the OUTPUT clause to log data changes
![Page 13: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/13.jpg)
Merge statementRequires a semi-colon
Can have up to 3 MATCHED (2 WHEN, 1 NOT “UPSERT” is misnomer, you can UPDATE, INSERT or DELETE in a MERGE statement
Introduced in 2008
![Page 14: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/14.jpg)
MERGE Statements
Demo
![Page 15: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/15.jpg)
Recursive Queries
Presented by Steve Stedman
![Page 16: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/16.jpg)
RecursionSELF-JOIN – some consider recursive
Common Table ExpressionsConsidered recursive when the CTE references
itselfRecursion stops
When the recursive query produces no results Or specify MAXRECURSION
![Page 17: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/17.jpg)
Uses for RecursionHierarchical DataTree PathRecursive Calculations (mathematic sequences)String ParsingFinding Holes
Replacing a Numbers TableDate Calculations
![Page 18: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/18.jpg)
Recursion
Demo
![Page 19: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/19.jpg)
Recursion Usage SummaryHierarchical DataTree PathRecursive Calculations (mathematic sequences)String ParsingFinding Holes
Replacing a Numbers TableDate Calculations
![Page 20: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/20.jpg)
Any Questions?Merge StatementData Recursion
Self JOINRecursive CTE
![Page 21: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/21.jpg)
For More InformationVisit http://EmergencyReporting.com to find
out more about Emergency Reporting.Aaron on the web
http://AaronBuma.comTwitter: @AaronDBuma
Steve on the webhttp://SteveStedman.comtwitter: @SqlEmt
![Page 22: SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin](https://reader034.fdocuments.us/reader034/viewer/2022042818/55c41713bb61eb20058b46e0/html5/thumbnails/22.jpg)
Tune in next weekThursday March 5th at
9:00am (pacific time).Topics - Review /
Lightning TalksNullExpressionsIdentityAggregationTop, Ranking and
Aggregation
OUTPUT ClauseDerived TablesMerge StatementSet OperatorsDerived TablesCommon Table
ExpressionsRecursive Queries
5 minutes per topic, followed by Q&A