Inline table valued functions in sql server Part 31 — Transcript
Full transcript
- 0:00hello welcome to preim Technologies I'm
- 0:02wenet this is part 31 of SQL server in
- 0:05this session we'll learn about creating
- 0:07and calling inline table valued
- 0:09functions where do we use inline table
- 0:12valued functions now before continuing
- 0:14with the session I strongly recommend to
- 0:15watch Part 30 of this video
- 0:18series in part 30 we have seen how to
- 0:21create and call Scala user defined
- 0:24functions in this part we'll learn about
- 0:27inline table valued functions we know
- 0:29that a scalar function returns a scalar
- 0:32value a single value whereas an inline
- 0:35table valued function returns a table
- 0:39now let's look at an example of creating
- 0:41an inline table valued function now if
- 0:44you look at this I've got an employee
- 0:46table which has got ID name date of
- 0:48birth gender and Department ID columns
- 0:51now I want you to write a function that
- 0:53can give me the output that you can see
- 0:55on the right hand side you know the
- 0:57function should return employees by
- 0:59gender for example if I passed your
- 1:01function gender as male I want only the
- 1:04male employees to be returned and along
- 1:06the same lines if I pass the gender as
- 1:08female and I only want the female
- 1:10employees to be returned okay and to do
- 1:13that we use the create function
- 1:15statement as we have used it to create
- 1:18Scala function okay so create if you
- 1:21look at the you know implementation of
- 1:23the function here it's very simple to
- 1:25understand so we use the create function
- 1:27statement so create function function
- 1:29name name and FN underscore employees by
- 1:33gender is the name of the function
- 1:35because we are going to return employees
- 1:36by gender and if you look at this
- 1:38obviously this function needs a
- 1:40parameter at gender of type NW care
- 1:44whose length is 10 okay since gender is
- 1:46a string we use nare data type and then
- 1:50this function returns table okay so this
- 1:54is the parameter that we are passing
- 1:55into this function at gender and this
- 1:57function returns stable if you remember
- 2:00in part 30 you know where we spoke about
- 2:02scalar user defined functions Scala
- 2:05function return a scalar data type for
- 2:07example maybe nare care or integer or
- 2:10date time you know a Scala value but we
- 2:13want this function now to return a list
- 2:15of employees by gender okay so obviously
- 2:18we want this function to return a table
- 2:20so we are specifying table as the return
- 2:23type from this function using the
- 2:26returns
- 2:26keyword and then we are using as keyword
- 2:31and return look at this for an inline
- 2:34table valued function we are not using
- 2:36the begin and end block if you remember
- 2:39for the Scala user defined function we
- 2:42had begin and end block the function
- 2:44body was enclosed within that block but
- 2:47for an inline table valued function it's
- 2:49a compile time error to have um you know
- 2:53basically the begin and end block you
- 2:55cannot have it for an inline table
- 2:57valued function so immediately after the
- 3:00ask keyword you specify the return and
- 3:03within brackets you specify your select
- 3:05statement okay so what do we want here
- 3:08we want ID name date of birth gender and
- 3:10Department ID columns so specify them in
- 3:12your select list from TBL employees
- 3:15table where gender equals at gender
- 3:20parameter whatever the user is going to
- 3:22pass in okay so we are using that
- 3:24parameter there and we are enclosing
- 3:26that entire select statement in
- 3:28parenthesis okay just I mean even if you
- 3:30don't indicate that we don't get an
- 3:32error but but it is more readable now
- 3:34after whatever is present in the in this
- 3:36parenthesis is completed return the
- 3:39output of the execution you know of the
- 3:42execution result of the select statement
- 3:45back to the calling application so when
- 3:46somebody calls your function return
- 3:49whatever your select statement is
- 3:51selecting
- 3:52here okay so if you notice there are
- 3:55three points to keep in mind when we
- 3:57compare inline table valued functions
- 3:59with Scala
- 4:00um user defined functions now with Scala
- 4:03functions I mean with inline table
- 4:04valued functions we specify table as the
- 4:07return type whereas for scalar functions
- 4:10we specify any of the scalar data types
- 4:13like integer andare Etc okay and for
- 4:17scalar functions you know here the
- 4:19function body is not enclosed between
- 4:21begin and end block the structure of the
- 4:23table this is very important now look at
- 4:26this you're telling your function it's
- 4:27going to return a table but what are the
- 4:30columns that are going to be present in
- 4:32the in that table you know basically the
- 4:34structure of the table we are not
- 4:36defining that here okay so the structure
- 4:39of the table will be determined by the
- 4:42select statement that you have so
- 4:43obviously the table that this function
- 4:45is going to return is going to have 1 2
- 4:473 4 five columns because your select
- 4:50query has five columns within its select
- 4:53list okay so the structure of the table
- 4:56that gets returned is determined by the
- 4:58select statement within the function
- 5:01body okay but later in the next session
- 5:04we'll be talking about multi-statement
- 5:06table valued function there we have the
- 5:08luxury of specifying the structure of
- 5:10the table within the return after the
- 5:13return keyword we'll talk about that in
- 5:15the next session okay so let's look
- 5:18quickly you know creating how to create
- 5:20this function so if you look at this I
- 5:23have this TBL employees table which is
- 5:25called The Columns that we have seen on
- 5:26the slide and to create the function we
- 5:28use the create function keyword the name
- 5:31of the function and then obviously the
- 5:33parameter that we want to pass along
- 5:35with its data type returns table you
- 5:38specify the ask keyword return and your
- 5:41select statement within brackets okay so
- 5:46let's execute this so when I execute
- 5:48command complet it successfully okay so
- 5:51where did this function get created if
- 5:53you look at the context we are executing
- 5:54this query in the sample database so
- 5:56once I expand sample database go to
- 5:58programmability and then to functions
- 6:01there you should see table valued
- 6:03functions in part 30 when we created
- 6:05Scala function that was stored within
- 6:08the Scala valued functions table but
- 6:10this is a table valued function an
- 6:12inline table valued function so that
- 6:15gets stored in this table valued
- 6:17functions folder if you can't see that
- 6:19just refresh it and expand the plus sign
- 6:22you should see the function that we have
- 6:24just created which is FN employees by
- 6:27gender okay
- 6:33now let us see how to call this function
- 6:35so obviously to call this function you
- 6:37know we use the syntax select star from
- 6:40the function name and then pass whatever
- 6:43parameters you have to pass to that
- 6:44function now this function returns a
- 6:47table so treat as if it is a table okay
- 6:51now if we want to select from a table we
- 6:54will say select star from that table
- 6:56name similarly this is an inline table
- 6:58valued function
- 7:00just issue a select statement against
- 7:01this inline table valued function as if
- 7:05you would issue a select statement
- 7:06against the table so select star from
- 7:09that function name okay so let's see how
- 7:14to execute that so select star from I
- 7:17want all the male employees so I'm
- 7:18passing in mail and when we execute this
- 7:21query we only get the male employees
- 7:23back and along the same lines if I want
- 7:25female employees I just pass female and
- 7:28execute the query so I I only get the
- 7:30female
- 7:32employees okay that's the syntax that we
- 7:34use to execute I mean to call the
- 7:36function now if you want you can even
- 7:40say use the we Clause you know from with
- 7:42a table we can use the we Clause even
- 7:45with the function you can use the we
- 7:46Clause okay this is a inline table
- 7:49valued function we get a table back so
- 7:51treat as if it's a table and you can
- 7:53just say where name is equal to maybe
- 7:57Pam I only get Pam's record now when we
- 8:00execute this all right okay so where can
- 8:04we actually use this inline table valued
- 8:07function okay um you can use I mean
- 8:10inline table valued functions can be
- 8:12used to achieve the functionality of
- 8:14parameterized views now what are what is
- 8:17a view and what are parameterized views
- 8:19we haven't spoken about them yet we'll
- 8:21be talking about them in our later
- 8:22sessions so until then don't worry about
- 8:25this point but just keep in mind that
- 8:27inline table valued functions are an
- 8:29exceptional replacement for
- 8:31parameterized views which we will talk
- 8:34about in detail in a later session okay
- 8:36and another place where you can actually
- 8:38use this inline table valued function
- 8:40you know is that you can actually use it
- 8:43in joints as well you know we know that
- 8:46the in line table valued function
- 8:47returns a table so the table returned by
- 8:50the table valued function can be used in
- 8:52Joins with other tables okay so if you
- 8:55look at this when we execute this query
- 8:57select star from function employees by
- 8:59agenda uh we got this result back since
- 9:02I'm passing in mail as the parameter
- 9:05value I get all male employees like this
- 9:07that you can see on the slide now if you
- 9:09look at the output we have got
- 9:11Department ID in The Returned table by
- 9:13this function okay now what I want you
- 9:16to do is I want you to join this return
- 9:18table with Department's table so that
- 9:22you can give me the output that you can
- 9:23see here I want the name of the employee
- 9:26their gender and their Department name
- 9:29if you look at the table returned by the
- 9:31function you don't have Department name
- 9:34in that table so obviously you will have
- 9:36to join the return the table with the
- 9:39Department's table okay and if you're
- 9:42new to joints we have already spoken
- 9:45extensively about joints in part 12 and
- 9:4713 of this video series please check
- 9:49those parts okay now if you look at the
- 9:52query here it's pretty simple just like
- 9:54how you join a table with another table
- 9:57we do exactly the same thing
- 10:00okay so first of all what are the
- 10:01columns that we want we want the name
- 10:03gender and Department name so you
- 10:05specify them in your select list select
- 10:07name gender Department name from you
- 10:11specify your function name passing it
- 10:13the parameter that you want here we want
- 10:16to pass in mail because we want the male
- 10:18employees so function name male
- 10:21employees and then give it an alas just
- 10:23like how you would give an alas to a
- 10:25table okay e for the function
- 10:30and then join TBL Department D on
- 10:35department. ID with employee. Department
- 10:40ID okay that's about it so you can even
- 10:43use the table that is returned by an
- 10:45inline table valued function in a
- 10:48join let's
- 10:50see the query so here you have that
- 10:53select name gender Department name from
- 10:55the function give it an alas join with
- 10:58TBL Department give it another alas D on
- 11:02department. ID equals employee.
- 11:05Department ID so when we execute that we
- 11:07get the output that we
- 11:15expect on this slide you can find
- 11:17resources for asp.net and cop interview
- 11:19questions that's it for today thank you
- 11:21for listening have a great day
About this transcript
This page contains the full transcript of Inline table valued functions in sql server Part 31 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,917 words across 273 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.