SQL SERVER – Table Valued Functions – Day 26 of 35

Let us learn about table valued functions. Every day one winner from the United States will get Joes 2 Pros Volume 4.

Table-Valued Functions

Scalar-valued functions return a single value. Table-valued functions return tabular result sets (“tabular” meaning like a table). Table-valued functions look a lot like views because they both show us a tabular result set based on an underlying query. Table-valued functions can be based on one or more base tables.

Creating and Implementing Table-Valued Functions

The body of a table-valued function will essentially contain a query. Let’s begin with a query containing four fields and all of the records from the CurrentProducts table.

This query will become the heart of a new table-valued function, GetAllProducts. By placing the query within the set of parentheses after the keyword RETURN, we have the body of the function. The RETURNS TABLE keyword specifies that the table-valued function GetAllProducts must return the result in the form of a table.

Just how do you query a table-valued function? The syntax is somewhat similar to how you would run a SELECT statement against a table or a view. All functions need to be called by using a set of parentheses with all required parameters inside them. If the function has no parameters (which is currently the case with GetAllProducts), then you will simply include an empty set of parentheses.

To view all of the table-valued functions contained in the JProCo database from within the Object Explorer tree, traverse this path:

Views versus Parameterized Table-Valued Functions

Views and table-valued functions are both useful ways to see the result set for a pre-defined query. There is no way to pass a variable into a view and change the way it runs. Views are hard-coded and their criterion does not change. A table-valued function can display different results by passing values into its parameter(s) at runtime. Let’s begin by selecting all ‘No-Stay’ records from the CurrentProducts table. We want to turn this query into a function and allow that function to pick the category.

We’re going to enclose our query in parentheses, indent it, and then add some code to create a function. We will create the GetCategoryProducts function which takes a @Category parameter. The query within the table-valued function will predicate on the value passed in when the function is called.

Change the parameter value to ‘Medium-stay’ and run this query. The GetCategoryProducts function now returns164 records.

SELECT * FROM GetCategoryProducts('Medium-stay')

Whenever you call a function, you must remember to use a set of parentheses and include the parameter(s) which the function expects. Let’s demonstrate the error message which results from forgetting to include the needed parameter within the parentheses. SQL Server’s error message tells us that our code doesn’t match the function’s parameter signature. In other words, it reminds us that we need to specify a category.

SELECT * FROM GetCategoryProducts()

Msg 313, Level 16, State 3, Line 1An insufficient number of arguments were supplied for the procedure or function GetCategoryProducts.

Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLProgrammingChapter8.1Setup.sql script from Volume 4.

Question 26

You need to create two functions that will each return a scalar result of the number of hours each user has logged for: 1) the current day, and 2) month to date. You will pass in the user ID as a parameter value. What two things must you do?

Create a function that returns a list of values representing the login times for a given user.

Create a function that returns a list of values representing the people who have logged more hours than the current user has logged.

Create a function that returns a numeric value representing the number of hours that a user has logged for the current day.

Create a function that returns a number value representing the number of hours that a user has logged for the current month.

I hope yo have enjoyed this blog post about table valued functions.

Rules:

Please leave your answer in the comment section below with correct option, explanation and your country of resident. Every day one winner will be announced from United States. Every day one winner will be announced from India.A valid answer must contain country of residence of answerer. Please check my facebook page for winners name and correct answer.Every day one winner from the United States will get Joes 2 Pros Volume 4. The contest is open till next blog post shows up at which is next day GTM+2.5.

3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4.Create a function that returns a number value representing the number of hours that

3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4.Create a function that returns a number value representing the number of hours that a user has logged for the current month.

3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4.Create a function that returns a number value representing the number of hours that a user has logged for the current month.

3. Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4. Create a function that returns a number value representing the number of hours that a user has logged for the current month.

Challenge: Question 26 You need to create two functions that will each return a scalar result of the number of hours each user has logged for: 1) the current day, and 2) month to date. You will pass in the user ID as a parameter value. What two things must you do?

1.Create a function that returns a list of values representing the login times for a given user. 2.Create a function that returns a list of values representing the people who have logged more hours than the current user has logged. 3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4.Create a function that returns a number value representing the number of hours that a user has logged for the current month.

Correct Answer: The correct choices are #3 and #4.

Explanation: Query #1 is incorrect. It will only return the login time for each user ID passed to it. The login time is only part of the solution (for a given login session). You also need to time the user logged off, then find the difference between the two values. Query #2 will provide a list of users who have logged more hours than the current user. While this result set may be useful, it doesn’t meet our requirements. Query #3 is the query we need to return the number of hours logged by the user ID for the current day. Query #4 is the query we need to return the number of hours logged by the user from the beginning of the month through today.

The correct answer is: 3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4.Create a function that returns a number value representing the number of hours that a user has logged for the current month.

3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day. 4. Create a function that returns a number value representing the number of hours that a user has logged for the current month.

Pinal Dave is a technology enthusiast and an independent consultant. He has authored 11 SQL Server database books, 21 Pluralsight courses and have written over 3800 articles on the database technology on his blog at a http://blog.sqlauthority.com. Along with 14+ years of hands on experience he holds a Masters of Science degree and a number of database certifications. For any SQL Server Performance Tuning Issue send email at pinal @ sqlauthority.com .

Nupur Dave is a social media enthusiast and and an independent consultant.