From the course: Intermediate SQL: Data Reporting and Analysis
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Using MERGE instead of UPDATE/INSERT - SQL Tutorial
From the course: Intermediate SQL: Data Reporting and Analysis
Using MERGE instead of UPDATE/INSERT
- SQL has been around since the seventies but the merge command was only entered into the SQL standard in 2008. So it's a relative newcomer. Merge does something quite special. It checks whether a record is present, based on a field you specify such as ID. If that record is present, it updates the fields to other fields that you specify. And if the record is not present, it inserts a row. So it's combining a two step process into one. For years, programmers have been checking whether the record is present with select, and then either running an update or insert command on that basis or indeed a delete command, which the merge query can also include. The problem with the two step process is that somebody else might come along and change your database in between the times it takes to execute those two statements. Even if they are only separated by a semicolon and run in the same window, there is still the opportunity for…
Contents
-
-
-
-
-
-
(Locked)
INSERT rows into a table6m 28s
-
(Locked)
Common issues with the INSERT command7m 8s
-
(Locked)
Using MERGE instead of UPDATE/INSERT8m 40s
-
(Locked)
Duplicate table with SELECT5m 28s
-
(Locked)
Challenge: Insert a row into the film table37s
-
(Locked)
Solution: Insert a row into the film table1m 45s
-
(Locked)
-
-