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.

SQL SERVER - Table Valued Functions - Day 26 of 35 joes2pros4

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.

SQL SERVER - Table Valued Functions - Day 26 of 35 j2p_26_1

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.

CREATE FUNCTION GetAllProducts( )
RETURNS TABLE
AS
RETURN
(SELECT ProductID, ProductName, RetailPrice, Category
FROM CurrentProducts)
GO

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.

SQL SERVER - Table Valued Functions - Day 26 of 35 j2p_26_2

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

OE > Databases > JProCo > Programmability > Functions > Table-valued Functions

SQL SERVER - Table Valued Functions - Day 26 of 35 j2p_26_3

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.

SQL SERVER - Table Valued Functions - Day 26 of 35 j2p_26_4

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.

SQL SERVER - Table Valued Functions - Day 26 of 35 j2p_26_5

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 1
An 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?

  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.

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.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Function, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Author’s Book is Available in India and USA
Next Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – SQL Server Error Messages – Day 27 of 35

Related Posts

73 Comments. Leave new

  • Kalyanasundaram.K
    August 26, 2011 7:24 am

    Correct Answer : Option 3 & 4

    We need two functions to get number of hours each user has logged for: 1) the current day, and 2) month to date.

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

    Function for Month to date
    4) Create a function that returns a number value representing the number of hours that a user has logged for the current month.

    Chennai, Tamilnadu, India

    Reply
  • 3) and 4)
    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) should gives hours for day
    4) should give hours for the month

    Leo Pius
    USA

    Reply
  • Hi,

    Option 3 will work for Current Day and Option 4 will work for Month to Date. 2 functions are required to achieve this.

    Option 1 and 2 are incorrect because they both return list of values and we are expected to create a function that return scalar value.

    Thanks

    Sudhir Chawla
    New Delhi, India

    Reply
  • The correct answers are 3 and 4
    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.

    because we want to create this function as a scalar function

    I am from USA

    Reply
  • Answer would have two option this time which is 3 & 4

    Ritesh (India)

    Reply
  • Aditya Bisoi (@AdityaBisoi07)
    August 26, 2011 9:05 am

    Question 26
    ANS :
    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.

    Chennai ,INDIA

    Reply
  • Hi,

    Answers are 3 and 4
    Since its specified that the funtion should return a scalar result

    Kerala, India

    Reply
  • Answer is option 3 and 4

    Shiv
    USA

    Reply
  • Rene Alberto Castro Velasquez
    August 26, 2011 9:23 am

    Correct answers are 3 and 4, because we need to return scalar values (one representing the number of hours each user has logged for the day, and the other one representing the number of hours each user has logged for month to date), and the other options return a list of values.
    Rene Castro
    El Salvador

    Reply
  • The correct answer options are 3 & 4

    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.

    Nikhildas
    cochin
    INDIA

    Reply
  • Hi,

    Correct Answer is
    Opetion 3 and 4

    Kaushik Modi,

    I am from Ahmedabad,Gujarat,India

    Reply
  • vishal patwardhan (@vishalpatwardha)
    August 26, 2011 10:45 am

    Answer: Option 3 and Optin 4

    Explanation:

    1)we’ll create a function eg: GetCurrentDayLoginHrs(@UserId int) this will returns the number of hours the users has been logged in current day as a scalar result.

    2)again we need to create a function eg:GetLoginHrsCurrentMonth(@UserId int) which in turns returns the total logging hours of the users for the current month.

    inside this we need to perform SUM(logging hours) for userid and where month would be current month.

    Vishal Patwardhan
    Indore(India)

    Reply
  • Correct Answer : Option 3 & 4

    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.

    City: Baroda

    Country: India

    Thanks
    GurjitSingh

    Reply
  • As per the question, the functions should take user ID as parameter and return number of hours for 1) Current Date 2) Current Month.

    Hence, we have to follow this steps.

    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.

    Thus, Option 3 & 4 are the correct answers.

    Ishan Shah,
    Gandhinagar,
    India

    Reply
  • 3 & 4 are correct

    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.

    Arjun,
    India

    Reply
  • answer is option 3 and 4
    India

    Reply
  • Nakul Vachhrajani
    August 26, 2011 11:14 am

    The correct answers are #3 & 4.

    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.

    Country of residence: India

    Reply
  • Rajneesh Verma
    August 26, 2011 11:19 am

    I will do:
    a) Create a function that returns a numeric value representing the number of hours that a user has logged for the current day.
    b) Create a function that returns a number value representing the number of hours that a user has logged for the current month.”
    Option.
    That means Option 3 and 4 are Correct Answers.

    Thanks…
    Rajneesh Verma
    (INDIA)

    Reply
  • Partha Pratim Dinda
    August 26, 2011 11:22 am

    Ans : 3 & 4

    To find how many hours the user logged on in the current day we have create this function
    3.Create a function that returns a numeric value representing the number of hours that a user has logged for the current day.

    And for total hours of the current month up to current date we have create this function
    4.Create a function that returns a number value representing the number of hours that a user has logged for the current month.

    Partha,
    India

    Reply
  • Hi,

    As the requirement is 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.

    The answers would be : 3 & 4
    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.

    Name: Hema Chaudhry
    Country : India

    Thanks,
    Hema Chaudhry

    Reply

Leave a Reply