YouTube2Text

Updatable common table expressions in sql server Part 50 — Transcript

by kudvenkat · 2,385 words · 331 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to prume Technologies I
  2. 0:02wenet this is part 50 of SQL server in
  3. 0:05this session we'll learn about updatable
  4. 0:08common table Expressions before
  5. 0:10continuing with the session I strongly
  6. 0:12recommend to watch part 49 of this video
  7. 0:14series where we had discussed the basics
  8. 0:16of Common Table
  9. 0:18Expressions we know that a Common Table
  10. 0:21expression is a temporary result set it
  11. 0:24can be considered as a derived table now
  12. 0:28is it possible to update a CTE the
  13. 0:30answer is yes and no under certain
  14. 0:33circumstances you can update a CTE but
  15. 0:36under certain other circumstances you
  16. 0:39cannot do that we will explore the you
  17. 0:42know scenarios where we can update a CTE
  18. 0:45and also where we cannot let's look at
  19. 0:47an example we have this TBL employee
  20. 0:50table which has got ID name gender and
  21. 0:51Department ID columns now let's say we
  22. 0:54want to create a CTE on this table which
  23. 0:58returns just the ID name and gender that
  24. 1:01you can see on the right hand side here
  25. 1:03so obviously we know that to create a
  26. 1:05CTE we use the with keyword and then we
  27. 1:08G we are giving it a meaningful name
  28. 1:10employees name agender because this CT
  29. 1:13returns name and gender so employees
  30. 1:14name and gender as and then the CTE
  31. 1:17query itself so select what are the
  32. 1:20columns we require ID name gender so ID
  33. 1:22name gender from TBL employee so this is
  34. 1:24a simple select query which returns ID
  35. 1:26name gender from TBL employee table and
  36. 1:29you giving that temporary result set a
  37. 1:32name employees name gender that's about
  38. 1:36it so when you created the CTE all
  39. 1:38you're doing here is Select star from
  40. 1:41that CTE which would return ID name
  41. 1:43gender from TBL employee as you can see
  42. 1:45on the right hand side now is it
  43. 1:48possible to update this CTE absolutely
  44. 1:52let's look at that so here what you're
  45. 1:53doing I mean it's the same CTA you know
  46. 1:56this CTE is pretty much similar to this
  47. 1:58one except that here you're selecting
  48. 2:01from that CTE but here you're updating
  49. 2:04the CTE so you're saying with employee
  50. 2:06name gender as select ID name gender
  51. 2:09from TBL employee now instead of saying
  52. 2:11select star from employee name gender
  53. 2:14you're actually updating the CTE so when
  54. 2:17you update the CTE does this actually
  55. 2:20update the underlying table in this case
  56. 2:23TBL employee so here you're setting the
  57. 2:26gender to female for the record with ID
  58. 2:30is equal to 1 so if you look at the
  59. 2:32record here ID is equal to 1 is John's
  60. 2:36record and he is male at the moment but
  61. 2:38then when I update this you know we
  62. 2:41basically changing John's gender from
  63. 2:44male to female so when you update the
  64. 2:46CTE look at this we are not directly
  65. 2:48updating the table TBL employee we are
  66. 2:51updating the CTE so when you update the
  67. 2:54CTE does it actually update the TBL
  68. 2:56employee table yes it does so when you
  69. 2:59actually execute this update statement
  70. 3:02along with the CTA it actually changes
  71. 3:05John's gender from male to female and
  72. 3:08this is from TBL employees table this is
  73. 3:10the base table all right so if a CT is
  74. 3:15created on one base table here this CTE
  75. 3:17is based on just TBL employee table then
  76. 3:20it is possible to update the CTE which
  77. 3:23in turn will actually update the
  78. 3:25underlying base table in this case we
  79. 3:28are updating employee named gender the
  80. 3:30CTE which is actually updating TBL
  81. 3:33employee table let's look at this in
  82. 3:37action so we have this TBL employee and
  83. 3:39TBL departments tables but then we are
  84. 3:42going to create a CTE on TBL employee
  85. 3:44table now look at this the select query
  86. 3:47returns ID name gender from TBL
  87. 3:50employee okay so for this temporary
  88. 3:53result set you're just giving a name
  89. 3:55called employee name gender so obviously
  90. 3:59when you execute this since this CTE is
  91. 4:02returning ID name gender we get that now
  92. 4:05let's see how to update the CTE so you
  93. 4:08have the same CTE but instead of saying
  94. 4:11select star from that employee names
  95. 4:13genda you're actually saying update
  96. 4:15employee names uh employees name gender
  97. 4:18said gender is equal to female where ID
  98. 4:20is equal to one so currently John's
  99. 4:22gender is male we are basically changing
  100. 4:25that to female with this statement I
  101. 4:27mean with the CT so you are actually
  102. 4:30updating the CTE and this CTE should
  103. 4:32update the TBL employee table let's look
  104. 4:34at that one row affected now let's check
  105. 4:38the TBL employee table itself you should
  106. 4:40see now John's gender is
  107. 4:44female all right so if a CTE is based on
  108. 4:46one base table then it is possible to
  109. 4:49update the CTE which in turn will update
  110. 4:52the underlying base table now let's look
  111. 4:54at a scenario when a CTE is based on two
  112. 4:57base tables now I have two tables here
  113. 4:59TBL employee and TBL department and then
  114. 5:02I want to create a CTE which would
  115. 5:04return ID name gender from TBL employee
  116. 5:07table and Department name from TBL
  117. 5:09Department table so obviously this CTE
  118. 5:12has to have two base tables okay so we
  119. 5:16are creating employees by Department CTE
  120. 5:19here as what you are selecting here ID
  121. 5:22name gender Department name and this is
  122. 5:23a pretty simple joins query you're
  123. 5:26joining TBL employee with TBL department
  124. 5:29on Department ID column which is common
  125. 5:31between these two tables and we are
  126. 5:33basically selecting ID name gender and
  127. 5:34Department name columns okay so for this
  128. 5:37temporary result set we're actually
  129. 5:39giving a name employees by department so
  130. 5:43this CTA is based on two base tables now
  131. 5:48okay but then if you look at the update
  132. 5:50statement look at the update statement
  133. 5:52so update employees by Department which
  134. 5:54is nothing but the CTA so we are
  135. 5:55updating the
  136. 5:56CTA update the CTA set
  137. 6:00gender is equal to male though the CT is
  138. 6:03based on two base tables we are only
  139. 6:05updating gender which is coming from TBL
  140. 6:08employee table so you're only updating
  141. 6:11you know this update statement affects
  142. 6:14only TBL employee table you're actually
  143. 6:16changing the gender to male for record
  144. 6:19with ID is equal to one so here John
  145. 6:22gender is now female because we have
  146. 6:24changed that before we we are trying to
  147. 6:27change it back to male using this CTE so
  148. 6:31if a CTA is based on more than one table
  149. 6:34that's what is the situation here the
  150. 6:36CTE is based on two base tables and if
  151. 6:39the update affects only one base
  152. 6:42table then the update is allowed okay
  153. 6:46let's look at that in
  154. 6:50action so we have the city the same
  155. 6:52query that we saw on the slide we are
  156. 6:54selecting ID name gender from employee
  157. 6:57Department by joining them on the
  158. 6:59department ID column so that's the CD
  159. 7:03which should give us the output that we
  160. 7:04have actually seen on the slide so now
  161. 7:07let's try to change John's gender from
  162. 7:11female to male so instead of saying
  163. 7:14select star from that CTE you know the
  164. 7:17CTE is exactly same from here to here
  165. 7:20instead of Select we are updating that
  166. 7:22CTE so update employees by Department
  167. 7:25said gender is equal to male where ID is
  168. 7:27equal to one in this case ID is equal to
  169. 7:29one is John's record so let's update
  170. 7:35this one row affected now let's check
  171. 7:39the employees table if it has actually
  172. 7:41changed John's gender from male female
  173. 7:45to
  174. 7:50male so you should see that John's
  175. 7:53gender now changed from female to male
  176. 7:56so if a CTE is based on two tables and
  177. 7:59if the update statement affects only one
  178. 8:01base table then the update is
  179. 8:07allowed all right let's look at another
  180. 8:10scenario so the CT here is based on two
  181. 8:12tables just like before okay it's the
  182. 8:15same CTE but then if you look at the
  183. 8:18update statement it's updating the
  184. 8:20gender it's changing the gender to
  185. 8:22female and it's also changing the
  186. 8:24department name column so if you look at
  187. 8:27this in this CT it has got two columns
  188. 8:30name and gender uh I mean gender and
  189. 8:33Department name okay so gender and
  190. 8:35Department name we know that this gender
  191. 8:37is coming from TBL employee table
  192. 8:39Department name is coming from TBL
  193. 8:41Department table so obviously when you
  194. 8:43execute this update statement on this
  195. 8:46CTE this update will actually affect TBL
  196. 8:50employee and TBL Department okay both
  197. 8:53the tables so if a CTE is based on
  198. 8:57multiple tables and if the update state
  199. 8:59M affects more than one base table then
  200. 9:02the update statement is not allowed
  201. 9:04let's try to do this and see what
  202. 9:06actually
  203. 9:09happens so here we have a CT that's
  204. 9:12based on two tables the same CT that we
  205. 9:15have been working with TBL employee and
  206. 9:17TBL Department which returns ID name
  207. 9:19gender and Department name columns but
  208. 9:21then if you look at the update statement
  209. 9:23we are changing the
  210. 9:24gender and the department name so
  211. 9:27obviously this update statement affects
  212. 9:31TBL employee and TBL department so when
  213. 9:33we try to execute this let's see what's
  214. 9:35going to happen we get an error so view
  215. 9:39of function employees by department is
  216. 9:42not updatable because the modification
  217. 9:44affects multiple base tables so
  218. 9:47basically if a CTE is based on multiple
  219. 9:50statements uh multiple tables and if the
  220. 9:52update statement affects more than one
  221. 9:55base table then the update is not
  222. 9:57allowed there is one one more scenario
  223. 10:00before we conclude okay so if a CTA is
  224. 10:04again based on two tables two or more
  225. 10:06tables actually and then if the update
  226. 10:09affects only one base table let's
  227. 10:11understand the scenario it's the same
  228. 10:12CTE that we have been working with okay
  229. 10:16so ID name gender from TBL Department
  230. 10:19table and Department name from TBL
  231. 10:21Department table so obviously this CT is
  232. 10:24based on more than one base table now if
  233. 10:27you look at this update statement
  234. 10:30so update employees by Department set
  235. 10:33Department name is equal to it so
  236. 10:36basically what you are saying you know
  237. 10:38when you're saying update this employees
  238. 10:40by Department this is the CT and you're
  239. 10:43basically
  240. 10:44saying Department name is equal to it
  241. 10:48set Department name is equal to it where
  242. 10:50ID is equal to 1 so you're basically
  243. 10:52telling okay change John's Department
  244. 10:56name from HR to it B basically telling
  245. 10:59that but then before we execute this
  246. 11:01update query let's look at Ben's
  247. 11:03Department as well Ben is also in the HR
  248. 11:06department so Ben and John are in the HR
  249. 11:09department before we update before we
  250. 11:12executed this update query now as soon
  251. 11:15as I have updated this now we know that
  252. 11:18if a CTE is based on more than one based
  253. 11:22on you know multiple base tables and if
  254. 11:25the update statement affects only one
  255. 11:27base table then the update is allowed
  256. 11:29okay so obviously this update will
  257. 11:31succeed but then after you execute this
  258. 11:35it changes John's Department name to it
  259. 11:38which is good but then look at what
  260. 11:39happened to Ben's Department name it is
  261. 11:42also changed to ID before this update
  262. 11:44statement Ben's Department was HR and I
  263. 11:47told to change here only John's
  264. 11:52Department to it because I specified ID
  265. 11:55is equal to 1 iD is equal to 1 is John
  266. 11:57but why did Ben Department name change
  267. 12:00as soon as I have executed this update
  268. 12:02statement that is because you know here
  269. 12:06you're asking this update statement to
  270. 12:08actually change the department name to
  271. 12:10ID where ID is equal to one so John you
  272. 12:13know Department name is currently HR so
  273. 12:16what's going to happen is when this
  274. 12:18update statement is executed it actually
  275. 12:21changes you know look at this department
  276. 12:24number three is a Char okay which means
  277. 12:27John is currently has got Department ID
  278. 12:303 which means HR but when this statement
  279. 12:32is completed when when you execute this
  280. 12:35what is happening is it is actually
  281. 12:38changing you know this label from HR to
  282. 12:42it so it it will be like this the TBL
  283. 12:46Department table will be like this so
  284. 12:48number three it Department ID 3 is now
  285. 12:52changed from hr2 it so obviously anybody
  286. 12:55who has got three as the department ID
  287. 12:58all of them will be shown as if they
  288. 13:00belong to you know it Department that's
  289. 13:02the reason why Ben is also shown as if
  290. 13:05he belongs to I know it Department
  291. 13:09here okay so if a c is based on more
  292. 13:13than one p base table and if the update
  293. 13:17affects only one base one base table
  294. 13:19then update succeeds but not as expected
  295. 13:23always you know it may work correctly in
  296. 13:25some cases but in in this case the up is
  297. 13:29not working as we expect it to work Our
  298. 13:32intention is to basically change you
  299. 13:34know when you're are saying change the
  300. 13:36CTA such that you know the department
  301. 13:39name of John is changed from HR to it
  302. 13:42when you say that behind the scenes it
  303. 13:45should actually update you know John's
  304. 13:47Department ID from 3 to one because one
  305. 13:50is ID 3 is HR but instead what it did it
  306. 13:54actually changed the label name in TBL
  307. 13:56Department table from h are to it
  308. 14:00because of which any employee who has
  309. 14:02caught a department ID of three will now
  310. 14:05be shown as if they belong to the IT
  311. 14:07department which is
  312. 14:08incorrect we didn't expect it to do that
  313. 14:11so obviously when you update a CT there
  314. 14:14are several scenarios so if a CTE is
  315. 14:16based on a single base table then the
  316. 14:19update succeeds and works as expected
  317. 14:21always but if a CT is based on more than
  318. 14:24one base table and if the update affects
  319. 14:26multiple base tables the dat is not
  320. 14:29allowed and the statement terminates
  321. 14:31with an error if a CTE is based on more
  322. 14:34than one base table and if the update
  323. 14:36affects only one base table the update
  324. 14:40succeeds you know but not as expected
  325. 14:45always on this slide you can find
  326. 14:48resources for asp.net cop and SQL server
  327. 14:50interview questions if you want to
  328. 14:52receive email alerts when I upload new
  329. 14:55videos please subscribe to my YouTube
  330. 14:57channel that's it for today day thank
  331. 14:59you for listening have a great day

About this transcript

This page contains the full transcript of Updatable common table expressions in sql server Part 50 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,385 words across 331 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.