Part 69 Merge in SQL Server — Transcript
Full transcript
- 0:00this is part 69 of sequel server
- 0:01tutorial in this video we will discus
- 0:04merge statement in sequel server what is
- 0:06the use of merge statement merge
- 0:08statement introduced in sequel server
- 0:092008 allows us to perform inserts
- 0:12updates and deletes in one statement
- 0:14this means we no longer have to use
- 0:16multiple statements for performing
- 0:18insert update and delete with merge
- 0:21statement we require two tables a source
- 0:23table and a target table source table is
- 0:26the table that contains the changes that
- 0:28needs to be applied to the target table
- 0:30target table is the table that require
- 0:33changes it is the table upon which we
- 0:35will be performing inserts updates and
- 0:37delat merge statement joins the target
- 0:40table to the source table by using a
- 0:42common colum in both the tes based on
- 0:45how the rows mat up we can then perform
- 0:47an insert update or delete on the target
- 0:50table let's look at an example now here
- 0:53we have got student source and student
- 0:55target tes student target table is the
- 0:57table on which we will be performing in
- 1:00update and delete all in one statement
- 1:02we will be using the id colum in both
- 1:05the tes to join them now when the rows
- 1:08mat between the tabl we want to perform
- 1:11an update on the target table look at
- 1:13the data that we have at the moment in
- 1:15both the tes we have a rec with id equ 1
- 1:19so those rs mat and we want to do an
- 1:21update meaning we want to upd m m to m
- 1:26so when r mat we want to perform update
- 1:30row do not mat that is when the rows are
- 1:32present in the source table but not in
- 1:35the target table we want to insert all
- 1:37such rows into the target table at the
- 1:40moment s record with id equ 2 is present
- 1:43only in the source table it not present
- 1:46in the target table so we want to insert
- 1:49that record into the target table and
- 1:51finally when the rows are present only
- 1:54in the target table but not in the
- 1:56source table we want to delete all su
- 1:59rows from the tg t at the with the tg t
- 2:04we have rec with id not in sce table so
- 2:08we want to delete it from the target
- 2:11table so after we execute the merge
- 2:13statement this is how we want the output
- 2:15to no m record is updated to m m updated
- 2:21to m s rec which is present only in the
- 2:24source table but not in the target table
- 2:26now inserted into the target table joh
- 2:29rec which present only in the target
- 2:31table but not in the source table is
- 2:33deleted from the target table and here
- 2:36is the syntax to do it so merge we
- 2:39specify the target table so in our case
- 2:41the target table is student target as
- 2:44and we give it an al t and then using
- 2:47the source table so in our case the
- 2:50source table is student source and we
- 2:52give it an alas s on we specify the join
- 2:56con so how are we joining these two tbl
- 2:59using the id colum in both the table so
- 3:02we are here we are using the ales t id =
- 3:06sid when matched so when the r mat based
- 3:10on that con then we want to perform an
- 3:12update on the target table so we are
- 3:15saying update set t name so name colum
- 3:19with the target table equals name colum
- 3:22from the s t so when the r mat perform
- 3:26an update when not matched by target
- 3:29this means when the rows are present in
- 3:32the source table but not in the target
- 3:35table then we want to do an iner so h we
- 3:39have iner statement iner we specify the
- 3:42colum name id and name and vales for
- 3:45those are going to come from source
- 3:48table s id and s name now by target is
- 3:53the defa so you may or may not spey that
- 3:57h when not matched by source this means
- 4:01when the rows are present in the target
- 4:03table but not in the source table then
- 4:07we want to perform a delete so pretty
- 4:10straight forward syntax there let's look
- 4:12at this in action let's flip to sequel
- 4:14server management studio so i have
- 4:16already created these two tbl here is
- 4:19the sequel script to create them i have
- 4:21the script on my blog in case you need
- 4:23it at the moment if you look at the data
- 4:26this is the target table and this is the
- 4:29source table after we execute this merge
- 4:32statement we want the data in the target
- 4:35table to be the same as that of the
- 4:37source table so let go execute this
- 4:39merge
- 4:41statement and let select the dat back
- 4:43look at that know the dark table is
- 4:46updated so m rec is updated s rec is
- 4:49inserted and john rec which was already
- 4:52present in the target table is deleted
- 4:55so iner update and delete all in a sle
- 5:01merge statement and one important thing
- 5:03to remember is that merge statement
- 5:05should end with a sem colon if not you
- 5:08will get an error so when we execute
- 5:10that look at that a merge statement must
- 5:12be terminated by a semicon so make sure
- 5:15you have one right in real
- 5:19time we mostly perform inserts and
- 5:22updates we don't delete data that is
- 5:25present only in the target table but not
- 5:26in the source table usually the source
- 5:29table provides us with new recs that are
- 5:31added and any recs that need to be
- 5:34updated okay so you know we only perform
- 5:38inss and updates and if that's
- 5:40requirement you can omit this last
- 5:42condition so it's the same quy as before
- 5:47except that we are deleting that last
- 5:50con so in this case what going to do
- 5:52going to perform an update for the
- 5:54matching r so m m will be updated to m s
- 5:57record which is present only in sce
- 5:59table but not in the destination table
- 6:01will be inserted into the target table
- 6:04but john record which is present only in
- 6:06the target table but not in the source
- 6:07table will be left you know untouched so
- 6:11it will not be deleted so here we are
- 6:13only doing an update and insert so let's
- 6:17look at that in action quickly so after
- 6:20we execute this merge statement you know
- 6:23m should be updated to m s record should
- 6:25be inserted and john record should be as
- 6:28it is so let flip toqu management stud
- 6:31now let actually tr the data that is
- 6:34already there with the source t and
- 6:36destination table let's execute these
- 6:39inert statement so we bring it back to
- 6:42how the data is before so that's our
- 6:45source and that's our destination and
- 6:48let's delete this last condition let's
- 6:52and this merge statement with a
- 6:54semicolon and let's execute
- 6:57this now let's select dat back no that m
- 7:01m upd m s rec ined and joh record is not
- 7:09delat for
- 7:12day
About this transcript
This page contains the full transcript of Part 69 Merge in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,192 words across 154 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.