YouTube2Text

Multi statement table valued functions in sql server Part 32 — Transcript

by kudvenkat · 2,371 words · 345 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to plenum technologies I
  2. 0:02am venket this is path 32 of sequel
  3. 0:04server in this session we'll learn about
  4. 0:06creating and calling multi statement
  5. 0:09table valued functions and finally we
  6. 0:11will also look at the differences
  7. 0:12between inline and multi statement table
  8. 0:15valued functions before continuing with
  9. 0:17the session I strongly recommand to
  10. 0:19watch parts 30 and 31 of this video
  11. 0:21series multi statement table valued
  12. 0:25functions are very much similar to
  13. 0:27inline table valued functions with a
  14. 0:28very few differences between them first
  15. 0:31let's look at an example of creating an
  16. 0:33inline and multi statement table valued
  17. 0:35functions and then we will find the
  18. 0:37differences between them I have this
  19. 0:39employee table which has got ID name
  20. 0:42date of birth gender and department ID
  21. 0:43columns now what we want to do is we
  22. 0:46want to create an inline and multi
  23. 0:48statement table valued functions which
  24. 0:50give us the same output that you can see
  25. 0:52on the right hand side I just want the
  26. 0:53ID name and date of birth okay so now to
  27. 0:56do that we are creating an inline table
  28. 0:58valued function here and we have spoken
  29. 1:00about inline table where it functions
  30. 1:03extensively in part 31 of this video
  31. 1:05series ok so to create this function we
  32. 1:08have fused create function and then
  33. 1:10function name returns table okay because
  34. 1:14we know that a function can take
  35. 1:15parameters and it should return a value
  36. 1:18okay so this function is not taking any
  37. 1:20parameters and it's returning a table
  38. 1:23and then we have this ask he word return
  39. 1:26whatever is the Select statement
  40. 1:28returning here the Select statement is
  41. 1:30selecting ID name and date of birth type
  42. 1:34casted as date from TBL employees table
  43. 1:37so whatever this select statement is
  44. 1:38retrieving from TBL employees table that
  45. 1:41is being returned by this function okay
  46. 1:43so this is an inline table valued
  47. 1:45function and if you look at the inline
  48. 1:47table valued function after returns you
  49. 1:50have only the table keyword you are not
  50. 1:52specifying the structure of the table
  51. 1:54that gets returned from this inline
  52. 1:57table valued function and another thing
  53. 1:59to note here is that this function FN
  54. 2:03ilt v underscore get employees this
  55. 2:05function doesn't have a begin and then
  56. 2:08clause okay now let's look at the multi
  57. 2:11statement table value function okay so
  58. 2:13whether
  59. 2:14if it is in line table valued function
  60. 2:16or scalar function or multi statement
  61. 2:18table valued function you always use
  62. 2:20create function statement to create a
  63. 2:21function so create function and then the
  64. 2:24name of the function here FN underscore
  65. 2:27multi statement table valued function
  66. 2:29underscore get employees M STV f stands
  67. 2:32for multi statement table valued
  68. 2:34function again the scope get employees
  69. 2:36and then I am saying returns at table so
  70. 2:42I am creating a table variable here okay
  71. 2:44so at table is the name of the variable
  72. 2:47and its data type is stable and
  73. 2:50obviously if you are creating a table
  74. 2:52variable a table variable has to have
  75. 2:55you know the column names and the data
  76. 2:57types so that's what we are creating
  77. 2:59here in the bracket okay when we are
  78. 3:01creating a table you will specify the
  79. 3:03name of the column and its data type
  80. 3:05similarly you know for a multi statement
  81. 3:08table valued function you are saying
  82. 3:10okay I am returning a table with this
  83. 3:13structure which has got ID name and date
  84. 3:16of birth okay so that's one of the
  85. 3:19primary difference between in line table
  86. 3:20valued function multi statement table
  87. 3:22valued function an in line table valued
  88. 3:24function you don't specify the structure
  89. 3:26of the table but in multi statement
  90. 3:28table valued function you do specify the
  91. 3:30structure of the table okay and then as
  92. 3:33and then you have this begin and end
  93. 3:36block but if you look at the in line
  94. 3:38table valued function you don't have
  95. 3:40that block okay and within that begin
  96. 3:42and end block you have the body of the
  97. 3:45function okay so what are you doing here
  98. 3:48here you are inserting data into this
  99. 3:51table variable you're saying insert into
  100. 3:54this table variable okay so what do you
  101. 3:57want to insert into this table variable
  102. 3:59you want to insert the ID name and date
  103. 4:03of birth type casted as date from the
  104. 4:06employees table so you are retrieving
  105. 4:07all these records you know from
  106. 4:09employees tables
  107. 4:10you're basically retrieving ID name and
  108. 4:12date of birth columns and then inserting
  109. 4:15those rows into this at table variable
  110. 4:19if you look at the at table variable it
  111. 4:21has got ID name and date of birth
  112. 4:23columns okay now if you look at date of
  113. 4:26birth here in this table it's a day
  114. 4:28den time column but here it's just a
  115. 4:31date column so that's the reason why I
  116. 4:33am typecasting this date of birth to be
  117. 4:34of type and not only that if you look at
  118. 4:37the output I just want the date I don't
  119. 4:39want the time part of it so that's why
  120. 4:41I'm just typecasting it to date type
  121. 4:43which will truncate that time part and
  122. 4:45just give me the date okay so you are
  123. 4:48inserting that into this table and
  124. 4:50finally you are saying return so when
  125. 4:52when this function encounters this
  126. 4:54return keyword what's going to happen
  127. 4:56this function will return this table so
  128. 4:59whatever you have inserted into this at
  129. 5:01table variable gets returned to the
  130. 5:04caller who ever calls your function he
  131. 5:07will get the data that is present in
  132. 5:08this table okay so if you look at the
  133. 5:10differences you know the difference is
  134. 5:12very simple to understand an inline
  135. 5:14table valued function you don't specify
  136. 5:16the structure of the table you just say
  137. 5:18it returns a table and the structure of
  138. 5:22the table that gets returned for an
  139. 5:25inline table valued function is
  140. 5:26determined by the Select statement that
  141. 5:28you have there
  142. 5:29okay so if you look at the multi
  143. 5:31statement table valued function you do
  144. 5:33define the structure for the table and
  145. 5:35then in line and other differences in
  146. 5:38line table valued function cannot have a
  147. 5:40begin and end block whereas a multi
  148. 5:42statement table valued function can have
  149. 5:45a begin and end block okay now before we
  150. 5:48continue to the you know added other
  151. 5:51differences between these two functions
  152. 5:52let's quickly create these functions and
  153. 5:55then see how to call them so to create
  154. 5:59an inline function look at this this
  155. 6:00exactly what we have seen on the slide
  156. 6:02create function function name returns
  157. 6:05table as return whatever this query
  158. 6:10returns look at this basically this
  159. 6:11query selects this data ID name date of
  160. 6:15birth okay so return this data from this
  161. 6:19function that's what we are exactly
  162. 6:20saying here so let's execute this
  163. 6:22function and create this function so the
  164. 6:24function got created so you go into that
  165. 6:27database programmability folder
  166. 6:30functions and since this is a table
  167. 6:32valued in line table valued function you
  168. 6:34look for that in the table valued
  169. 6:36functions folder refresh that you should
  170. 6:39see the function that we have just
  171. 6:40created and all
  172. 6:42obviously to call this function you will
  173. 6:44say select star from that function name
  174. 6:47so what's that function name FN
  175. 6:50underscore IL TV and score get employee
  176. 6:56so when I press f5 I get the result back
  177. 6:58as expected and along the same lines if
  178. 7:01I want to create a multi statement table
  179. 7:03valued function you use the create
  180. 7:05function create function function name
  181. 7:08returns a table variable which has got
  182. 7:12this structure and you are seeing this
  183. 7:15variable is of type table and then as
  184. 7:18begin and end blocks within that you are
  185. 7:21saying okay into this table variable
  186. 7:23insert ID name and date of birth from
  187. 7:27TBL employees so this table has the rows
  188. 7:31that are present in TBL employees but
  189. 7:33only ID name and date of birth columns
  190. 7:35and finally you are saying return which
  191. 7:38will return this table variable back to
  192. 7:40the caller so let's create this function
  193. 7:43and obviously to select data from this
  194. 7:47function you will say select star from
  195. 7:50in line table valued functions and multi
  196. 7:53statement table valued functions both of
  197. 7:54them will be in this table valued
  198. 7:56functions folder so this is our multi
  199. 7:58statement table valued functions let's
  200. 8:00drag and drop it and both I mean to call
  201. 8:05an in line table valued function or a
  202. 8:07multi statement table valued function
  203. 8:08you use the same syntax select star from
  204. 8:11the function name select star from the
  205. 8:14function name so let's plus a5 and you
  206. 8:16should see the output there okay
  207. 8:19so we have seen how to create and call
  208. 8:21an in line table valued function and a
  209. 8:24multi statement table valued function
  210. 8:25now let's find out the differences
  211. 8:27between them okay we have already seen a
  212. 8:30few of the differences in an in line
  213. 8:32table valued function the returns class
  214. 8:34cannot contain the structure of the
  215. 8:36table the function is going to return
  216. 8:38whereas with multi statement table
  217. 8:40valued function we specify the structure
  218. 8:43of the table that gets returned we have
  219. 8:44seen that in line table valued function
  220. 8:47cannot have begin and end blocked
  221. 8:48whereas the multi statement table valued
  222. 8:50function can have that in line table
  223. 8:53valued functions this is import
  224. 8:55and okay now if you look at what we have
  225. 8:58done just now in the previous slide you
  226. 9:00know this output that I want to achieve
  227. 9:02I can achieve that both with an inline
  228. 9:05table valued function as well as a multi
  229. 9:07statement table valued function so when
  230. 9:09you have two choices to do the same work
  231. 9:11I would always prefer an inline table
  232. 9:14valued function over multi statement
  233. 9:16table valued function because of the
  234. 9:18performance gain you know that I get
  235. 9:20when I use inline table valued functions
  236. 9:22so in line table valued functions are
  237. 9:25better for performance than multi
  238. 9:27statement table valued functions so if
  239. 9:29the given task can be achieved using
  240. 9:30both then always prefer in line table
  241. 9:33valued functions and the reason for
  242. 9:36improved performance and in line table
  243. 9:37valued function is because internally
  244. 9:40sequel a sequel server treats an inline
  245. 9:43table valued function much like it
  246. 9:44would've you okay
  247. 9:47whereas a multi statement table valued
  248. 9:50function you know is treated similar
  249. 9:53like it would a stored procedure that's
  250. 9:57why in line table valued functions are
  251. 9:59better for performance reasons okay
  252. 10:03another difference between an inline
  253. 10:05table valued function and multi
  254. 10:06statement table weld function is that
  255. 10:08it's possible to underlying table using
  256. 10:12an inline table value function but it's
  257. 10:14not possible with multi statement table
  258. 10:16value function let's understand what we
  259. 10:19mean by this now if you look at this
  260. 10:20inline table valued function this
  261. 10:24function is actually getting its data
  262. 10:26from the employees table so for this
  263. 10:29function the underlying table is nothing
  264. 10:31but the employees table now look at this
  265. 10:34to retrieve the data from this function
  266. 10:36I am calling it as if it is a table so I
  267. 10:40am saying select start from this
  268. 10:42function you know I'm we are almost
  269. 10:44treating it like a table okay now if
  270. 10:48when I call this function this function
  271. 10:50actually execute this statement and
  272. 10:52returns that data back to me and and
  273. 10:55this statement is being executed on TBL
  274. 10:58employees table so for this function
  275. 11:00this employees table is the underlying
  276. 11:01table now I am able to retrieve the data
  277. 11:04from TBL employee's table through this
  278. 11:07function now is it possible to add
  279. 11:09and update the data through this
  280. 11:11function absolutely you can do that
  281. 11:13using an inline function let's do that
  282. 11:16and see what happens so I'm saying
  283. 11:17update this function okay update this
  284. 11:23function said you know I want to set
  285. 11:26Sam's name from Sam one to maybe Sam set
  286. 11:31name is equal to Sam where Sam's ID is 1
  287. 11:40so let's say ID is equal to 1 and see if
  288. 11:44it updates that so when I execute this
  289. 11:47let's see what happens now look at this
  290. 11:49it says one row affected which means the
  291. 11:51true got updated so now let's say select
  292. 11:54star from the table and you see that Sam
  293. 11:57has been updated to Sam one has been
  294. 11:59updated to Sam as expected now let's try
  295. 12:02to do the same thing if you look at this
  296. 12:05you have this multi statement table
  297. 12:07valued function now when I try to
  298. 12:09retrieve data from that obviously I get
  299. 12:12the output you know the same output but
  300. 12:15can is you an update statement against
  301. 12:18the State against this function and
  302. 12:21update the underlying table it's not
  303. 12:23possible ok but let's try to do that and
  304. 12:26see what happens
  305. 12:27ok instead of rewriting the query let's
  306. 12:30copy this same query and change the name
  307. 12:33of the function so instead of updating
  308. 12:35the inline table valued function we want
  309. 12:38to update the multi statement table
  310. 12:39value function so let's do that and see
  311. 12:45what's going to happen so I want to
  312. 12:46update Sam to maybe Sam one I press F I
  313. 12:51look at this object you know function
  314. 12:54multi statement table valued function
  315. 12:56get employees cannot be modified and it
  316. 12:59makes sense why if you look at the
  317. 13:01implementation of the multi statement
  318. 13:02table valued function this function is
  319. 13:05not directly getting its data from TBL
  320. 13:07employees I mean it's getting from TBL
  321. 13:09employees but then it it is being
  322. 13:12inserted into this table variable and
  323. 13:14that table variable is getting you know
  324. 13:16returned back to the caller ok
  325. 13:19so for that reason you know in a multi
  326. 13:21statement table valued funk
  327. 13:22as you can have many intermediate
  328. 13:24processing steps there you don't really
  329. 13:27know which column is coming from which
  330. 13:29table and and from which fine
  331. 13:32you know variable we are finally
  332. 13:33returning the data back so there will be
  333. 13:35many intermediate processing steps there
  334. 13:38and it doesn't really make sense in
  335. 13:40allowing to update the underlying tables
  336. 13:42using the multi statement table valued
  337. 13:45functions and that's the reason why
  338. 13:46sequel server doesn't allow us to do
  339. 13:48that okay so that's another difference
  340. 13:51between multi statement table valued
  341. 13:53function and inline table valued
  342. 13:54function on this slide you can find
  343. 13:58resources for asp.net and C sharp
  344. 14:00interview questions that's it for today
  345. 14:02thank you for listening

About this transcript

This page contains the full transcript of Multi statement table valued functions in sql server Part 32 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,371 words across 345 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.