YouTube2Text

Scalar user defined functions in sql server Part 30 — Transcript

by kudvenkat · 3,105 words · 437 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to preim Technologies I'm
  2. 0:02wenet this is part 30 of SQL server in
  3. 0:05this session we'll learn about user
  4. 0:07defined functions and their types
  5. 0:09creating and calling a scaler user
  6. 0:11defined function places where we can use
  7. 0:14these and finally how to alter and drop
  8. 0:16a user defined function from Parts 22 to
  9. 0:2029 we have learned how to use many of
  10. 0:23the buil-in system functions that are
  11. 0:25available in SQL server in this session
  12. 0:27we'll turn our attention to creating
  13. 0:30user defined functions user defined
  14. 0:32functions are also called as
  15. 0:35udfs in SQL Server there are three types
  16. 0:38of user defined functions scalar
  17. 0:40functions inline table valued functions
  18. 0:42and multi-statement table valued
  19. 0:44functions we will be talking about
  20. 0:46inline and multi-statement table valued
  21. 0:48functions in a later session in this
  22. 0:51session we'll concentrate on how to
  23. 0:53create Scala functions now before you
  24. 0:56know looking at how to create a Scala
  25. 0:58function let's look at how to use one of
  26. 1:01the existing system functions now all of
  27. 1:03the system functions that we have worked
  28. 1:05from Parts 22 to 29 are actually Scala
  29. 1:09inbuilt system functions okay for
  30. 1:12example we have the square function so
  31. 1:15let's try to use that and to use that we
  32. 1:17use the select keyword and then Square
  33. 1:20the name of the function itself and when
  34. 1:22I open this parenthesis look at the
  35. 1:24intelligence of SQL Server management
  36. 1:26Studio it shows that this function is
  37. 1:30expecting a float parameter and it and
  38. 1:33it returns a float data type a float
  39. 1:37value okay so if we give it a number for
  40. 1:40example let us say I pass in three so
  41. 1:43this function is going to square that
  42. 1:45function and return that the square back
  43. 1:48to us in the form of float so if you
  44. 1:51understand the functions have the
  45. 1:53capability of taking in parameters do
  46. 1:57some processing and return some value
  47. 1:59values back okay so when we execute this
  48. 2:03we get nine now is it mandatory for a
  49. 2:06function to have a parameter no we have
  50. 2:08also looked at many functions which
  51. 2:10doesn't take parameters at all for
  52. 2:12example if you take the get date method
  53. 2:15which Returns the current system date
  54. 2:17time it doesn't take any parameter look
  55. 2:19at this this function doesn't expect any
  56. 2:22parameter it Returns the current date
  57. 2:24and time on that SQL Server instance
  58. 2:28okay when we press a five we get the
  59. 2:30current date and time okay so a function
  60. 2:34can have parameters that's optional but
  61. 2:36a function should always return a value
  62. 2:39okay so now let's go ahead and see how
  63. 2:42to create a user defined Scala function
  64. 2:45so what is a scalar function a Scala
  65. 2:47function is a function that takes zero
  66. 2:50or more parameters and returns a value
  67. 2:53okay a function may or may not have
  68. 2:55parameters but it should return a value
  69. 2:59okay uh since a since a scalar function
  70. 3:02returns a single scalar value it's
  71. 3:04called a scalar function okay and to
  72. 3:07create a scalar function we use the
  73. 3:08following syntax create function
  74. 3:12function name now to create a St
  75. 3:14procedure we will use create procedure
  76. 3:16procedure name similarly to create a
  77. 3:18table we use create table table name to
  78. 3:21create a function create function
  79. 3:23function name and then we know that a
  80. 3:26function can have parameters So within
  81. 3:29the parenthesis is you specify the name
  82. 3:31of the parameter and its data type okay
  83. 3:34you can have you know up to 1024
  84. 3:37parameters okay and then a function will
  85. 3:41also return a value so to specify what
  86. 3:44is this function going to return we use
  87. 3:46the returns keyword and then the data
  88. 3:48type okay now if you look at the square
  89. 3:51function it returns a float data type
  90. 3:54whereas get date function returns a date
  91. 3:56time data type now the function that we
  92. 3:59are going to to write you know whatever
  93. 4:01data type that it's going to return you
  94. 4:03specify the data type after the returns
  95. 4:06keyword now remember a function can
  96. 4:09written almost any of the data types
  97. 4:11that we have any of the scalar data
  98. 4:13types that we have except text and text
  99. 4:16image cursor and date time and time
  100. 4:18stamp a function cannot return these
  101. 4:21data types that you can see here okay
  102. 4:24and then as begin end just like the St
  103. 4:28procedure the body of of the function
  104. 4:31goes inside this begin and end
  105. 4:33statements and finally remember your
  106. 4:36function should return back the return
  107. 4:38return you know the data type you
  108. 4:40specified here okay for example if you
  109. 4:42are Computing the age of a person then
  110. 4:45it should return an
  111. 4:47integer okay so obviously if we have to
  112. 4:50write a function to compute the age of
  113. 4:52the person now obviously to compute the
  114. 4:54age we require to know the date of birth
  115. 4:57of a person so we need to pass in dat of
  116. 4:59birth as the parameter to that function
  117. 5:01and it's going to return an integer back
  118. 5:04which is nothing but the number of years
  119. 5:06all right so let's see how to create a
  120. 5:09user defined function that computes the
  121. 5:11age of the
  122. 5:12person okay
  123. 5:15now let's get rid of these function
  124. 5:18calls here now if you look at this I
  125. 5:20have written this code already uh just
  126. 5:23to save some time in typing if you look
  127. 5:25at this I've created a variable called
  128. 5:27at doob of type date and there is
  129. 5:31another variable of type integer age
  130. 5:33which is used to hold the age of the
  131. 5:34person and I have initialized the date
  132. 5:36of birth to something uh 8th October
  133. 5:401982 now how do we compute the age of
  134. 5:42the person we have already spoken about
  135. 5:44this in part 27 of this video series
  136. 5:48okay but let's quickly review it so I'm
  137. 5:50using the date diff function and then we
  138. 5:52are saying okay compute the difference
  139. 5:55in years give me the year part whatever
  140. 5:57is the difference in years and then this
  141. 5:59is my start date and this is my end date
  142. 6:02compute the difference between them in
  143. 6:04terms of years okay so that will give us
  144. 6:07the ear part now you might be wondering
  145. 6:10okay look at this let's comment this out
  146. 6:13this is not
  147. 6:14required if this is not there what's
  148. 6:17going to happen let's try to run this
  149. 6:19look at this if I run this I still get
  150. 6:22the age okay but then there is slightly
  151. 6:24a problem here now
  152. 6:28today the date is September 3rd
  153. 6:322012 okay so if it is September 2012
  154. 6:36September 3rd 2012 now let us say I am
  155. 6:41born on
  156. 6:44maybe
  157. 6:45nov
  158. 6:4988th 2011 so if it's November 8th 2011
  159. 6:54this is 2012 September and if I execute
  160. 6:57this I get ages one year I haven't even
  161. 7:00completed one year okay so it just
  162. 7:04computes the difference in years without
  163. 7:06consideration to the days and months
  164. 7:08here that's why we get that you know
  165. 7:11when I haven't completed actually one
  166. 7:13year it returns one year so obviously we
  167. 7:16need this case statement to compute the
  168. 7:18age correctly so what we are doing here
  169. 7:20is basically okay from whatever is the
  170. 7:24difference between the ears you know we
  171. 7:26are using this case statement and we are
  172. 7:28subtracting one or zero okay when do we
  173. 7:31subtract one from the difference in
  174. 7:33years whenever the date of birth month
  175. 7:37is greater than the current system date
  176. 7:39and time or if the months are equal and
  177. 7:42if the day of the date of birth is
  178. 7:44greater than the day of get date now the
  179. 7:47logic is not that important here because
  180. 7:49our you know we are here to learn about
  181. 7:51scalar functions okay so if this is
  182. 7:53confusing please watch part 27 where we
  183. 7:56have clearly walked through the logic of
  184. 7:58how to calculate the number of I mean
  185. 8:00the age of a person all right so finally
  186. 8:03I mean this code will give us the exact
  187. 8:06age so if I execute that it tells me you
  188. 8:08haven't even completed one year okay so
  189. 8:11that's why it says zero which correctly
  190. 8:13computes that so this is a bit of
  191. 8:14transact SQL code that I have now how do
  192. 8:17I convert this code into a function okay
  193. 8:21now if you look at this I want to kind
  194. 8:23of make this code into a function which
  195. 8:25takes in date of birth as the input
  196. 8:28parameter calculate Ates his age and
  197. 8:31returns that back as an integer so
  198. 8:33obviously to create a function we use
  199. 8:36the create function statement so create
  200. 8:38function and then the function name so
  201. 8:41let's say I want to create you know
  202. 8:43maybe a function
  203. 8:45like
  204. 8:48calculate H obviously a function you
  205. 8:52know this function requires date of
  206. 8:55birth as the input parameter so let's
  207. 8:57say add DOB and this has to be date
  208. 9:01parameter and it should
  209. 9:05return back an integer which is nothing
  210. 9:08but our age of the person as begin and
  211. 9:13finally end okay so if you look at the
  212. 9:18syntax here create function function
  213. 9:20name parameters returns data type as
  214. 9:23begin end and within the begin and end
  215. 9:27the function body goes in and finally
  216. 9:29you should have a return statement okay
  217. 9:32so instead of Select you should say you
  218. 9:35want to return the age back whoever
  219. 9:37calls this function return the age back
  220. 9:39to that calling program okay and we
  221. 9:42don't have to have this initialization
  222. 9:44here anymore because the end user is
  223. 9:47going to pass in the date of birth so
  224. 9:49you can get rid of that you don't even
  225. 9:52require this declaration of the variable
  226. 9:54here because you have the
  227. 9:56parameter so that's our function so when
  228. 9:59we execute this what happens this
  229. 10:01function gets created in the sample
  230. 10:03database okay so if I expand the
  231. 10:06database if we go to the sample database
  232. 10:09programmability and functions and this
  233. 10:12is a scalar function why is it called a
  234. 10:14scalar function because it returns a
  235. 10:15single
  236. 10:17value okay so that's why it's a scalar
  237. 10:19function we will talk about inline and
  238. 10:21multi-statement table valued functions
  239. 10:23in the next session and if I expand
  240. 10:26Scala functions here scalar valued
  241. 10:28functions and if if I refresh this you
  242. 10:31should see calculate age function there
  243. 10:34that we have written and look at the
  244. 10:35name it says dbo do calculate age that's
  245. 10:39nothing but dbo stands for database
  246. 10:41owner and then dot calculate age is the
  247. 10:44name of a function and this function is
  248. 10:46actually present in Sample database so
  249. 10:49the fully qualified name of this
  250. 10:51function is sample. db. calculate age
  251. 10:56now remember to invoke a user defined I
  252. 10:59mean a Scala user defined function you
  253. 11:01need to specify at least the two-part
  254. 11:04name now let's you know kind of invoke
  255. 11:07this function to invoke this function
  256. 11:09just like how we have invoked get date
  257. 11:11and power and square functions we do it
  258. 11:15the same
  259. 11:16way but we need to use a two-part name
  260. 11:20okay let's understand that let's say my
  261. 11:22date of birth is you know
  262. 11:26maybe October 88th
  263. 11:301982 now look at this I'm just saying
  264. 11:33select calculate age and when I execute
  265. 11:36this it says calculate age is not
  266. 11:38recognized as a built-in function name
  267. 11:40that's because whenever you invoke a
  268. 11:42scalar user defined function you need to
  269. 11:44specify at least the two-part name
  270. 11:47database owner dot the name of the
  271. 11:49function here the database owner is dbo
  272. 11:53so now when we press F5 we should get
  273. 11:56the age back and obviously if you want
  274. 11:58to give it a a column name you can use
  275. 12:01and a column alas so when I press F5 I
  276. 12:04get age 29 now you can also use the
  277. 12:07fully qualified name which is nothing
  278. 12:09but sample. dbo do calculate
  279. 12:17age so even that
  280. 12:20works all right so we have seen how to
  281. 12:23create that um you know age
  282. 12:26function and then we also have seen how
  283. 12:28to invoke that function now to invoke a
  284. 12:30scal user defined function you must
  285. 12:32Supply at least the two-part name owner
  286. 12:34name do function name and you can also
  287. 12:37invoke it using the complete three-part
  288. 12:40name okay now let's look at you know how
  289. 12:43to use this user Define function we have
  290. 12:46seen you know we have used it you know
  291. 12:48with the select statement not only that
  292. 12:50if you have I mean if you remember we
  293. 12:52have been working with a table called
  294. 12:54TBL
  295. 12:56employees okay so if you look at this we
  296. 12:59have this table TBL employees where we
  297. 13:01have got the ID name and date of birth
  298. 13:03of the person now I want you to write a
  299. 13:06query which will give me the name date
  300. 13:08of birth and age of the person and
  301. 13:10obviously we have a function and look at
  302. 13:12this in the database we are not storing
  303. 13:14the age of the person but we have date
  304. 13:16of birth So based on the date of birth
  305. 13:18we can actually calculate the age of the
  306. 13:20person and we have written a scalar
  307. 13:21function for that so what we are going
  308. 13:24to do here okay we want name date of
  309. 13:26birth so name date of birth and then the
  310. 13:28age of the person to get that we are
  311. 13:30using the age function okay db. Ag and
  312. 13:34to that function we are passing in you
  313. 13:37know the date of birth column so this
  314. 13:39function is going to calculate age for
  315. 13:42every person and return that okay so you
  316. 13:45can use the Scala user defined Scala
  317. 13:48function in the select Clause of a query
  318. 13:50not only in the select Clause you can
  319. 13:53also use it in the we Clause if you see
  320. 13:54the second example here now I want all
  321. 13:57the people in all the employees whose
  322. 14:00age is greater than 30 is that possible
  323. 14:03absolutely just like how you use the
  324. 14:05function in the select Clause you can
  325. 14:07also use it in the we Clause look at
  326. 14:09this I'm selecting name date of birth
  327. 14:11and age from TBL employees where I'm
  328. 14:16using the function again age of date of
  329. 14:19birth greater than 30 and that returns
  330. 14:22me the employees whose age is greater
  331. 14:24than 30 okay let's quickly see how to
  332. 14:26execute these queries so select
  333. 14:30I want the
  334. 14:32ID name and dbo do
  335. 14:38calculate age and we have the date of
  336. 14:42birth column we want to give an alas as
  337. 14:45AG when we press F5 we should get the ID
  338. 14:48name and age of the person and similarly
  339. 14:51if you want just people's people whose
  340. 14:55age is greater than 30 all you do is you
  341. 14:58will say okay
  342. 14:59whoever age which is given to us by the
  343. 15:03calculate age function is greater than
  344. 15:0630 so when we execute that we should
  345. 15:08only get the two people whose age is
  346. 15:10greater than 30 so it's also possible to
  347. 15:13use it in the we Clause okay now if you
  348. 15:16look back we have also spoken about St
  349. 15:18procedures in the past now whatever we
  350. 15:21have achieved using this Scala calculate
  351. 15:24age function we can also achieve it
  352. 15:27using store procedures we can write sto
  353. 15:29procedure which takes in I mean if you
  354. 15:32want you can quickly
  355. 15:36convert this function by the way if you
  356. 15:39want to look at the text of this
  357. 15:41function you can use the SP help text
  358. 15:44function we have seen you know the
  359. 15:46purpose of this function we have used it
  360. 15:49with stor procedures as well if you want
  361. 15:51to look at the text of a procedure you
  362. 15:53can use Spore help text procedure name
  363. 15:57similarly if you want to look at the
  364. 15:59text of the function Spore help text and
  365. 16:02the function name so now if I look at
  366. 16:06this look at this this is the function
  367. 16:08implementation now instead of saying I
  368. 16:11want a function I can say create
  369. 16:13procedure I can also convert this to a
  370. 16:15procedure create procedure let's call
  371. 16:18this SP calculate age and
  372. 16:23then we will say this procedure is going
  373. 16:26to take at date of birth date and we
  374. 16:29wouldn't say it returns as
  375. 16:32begin and finally instead of returning
  376. 16:36we will just say select age so when I
  377. 16:38execute this what happens we have the St
  378. 16:40procedure created SP calculate age and
  379. 16:43obviously if you want to execute that we
  380. 16:45use
  381. 16:46execute again if you're new to St
  382. 16:49procedures please check our video
  383. 16:50tutorial on St procedures that we
  384. 16:52covered in the past and if we pass you
  385. 16:55know some date of birth to this
  386. 16:59it's going to compute the age and select
  387. 17:01that for us okay so whatever we have
  388. 17:05achieved using a userdefined function we
  389. 17:07can also do that with stor procedures
  390. 17:09okay but the places where you can use a
  391. 17:14function okay you cannot do that with
  392. 17:16store procedures for example we have
  393. 17:17just seen that a function can be used in
  394. 17:20the select and wear Clause but can I use
  395. 17:22a store procedure in the in the select
  396. 17:24and we Clause no you cannot do that if
  397. 17:26you try to do that you will get an error
  398. 17:28so select uh maybe
  399. 17:32ID from
  400. 17:34TBL employees table so what we want we
  401. 17:38want the ID
  402. 17:40name and if I wanted the age we have
  403. 17:44used that function calculate AG to which
  404. 17:48we have passed the date of birth
  405. 17:51column okay so if we executed this we
  406. 17:54got the age without any problem but can
  407. 17:56I do that with store procedures instead
  408. 17:58of using the function here in the select
  409. 18:00list can I use the stor procedure you
  410. 18:03cannot okay if you try to do that you
  411. 18:07will get an
  412. 18:08error because look at this the way you
  413. 18:10pass parameters to store procedure I
  414. 18:12mean they are there for a different
  415. 18:14purpose okay so that's the advantage of
  416. 18:17scalar functions and this is one of the
  417. 18:19differences between a scalar user
  418. 18:21defined function and a St procedure a
  419. 18:23Scala user defined function can be used
  420. 18:25in the select and wear class whereas a
  421. 18:27store procedure cannot be but there are
  422. 18:29several other important differences
  423. 18:31between scalar defi Scala user defined
  424. 18:34functions and store procedures which we
  425. 18:36will be talking about in a later session
  426. 18:39okay to alter a function we use alter
  427. 18:41function statement and similarly to
  428. 18:43delete it we use the drop function
  429. 18:45statement just like table or I mean
  430. 18:48store procedure or any other database
  431. 18:50object we use the same statements alter
  432. 18:52function function name drop function
  433. 18:55function
  434. 18:57name on this slide you can find
  435. 18:59resources for asp.net and cop interview
  436. 19:01questions that's it for today thank you
  437. 19:03for listening have a great day

About this transcript

This page contains the full transcript of Scalar user defined functions in sql server Part 30 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 3,105 words across 437 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.