Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining a table value function to a MSSQL query

Tags:

sql-server

I have a table-value function that takes an ID number of a person and returns a few rows and columns. In another query, I am creating a SELECT that retrieves a lot of information about many people. How can I pass an id number from my main query to my function to sum a column and join it to my main query? I wish I didn't have a table value function since that would work easily, however, this function is used elsewhere and I'd like to reuse it. Perhaps this isn't even possible with a table-value function and I need to create a scalar one.

My main Query looks like this:

select id_num, name, balance  from listOfPeople 

And the table-value function looks like this:

calculatePersonalDiscount(id_number) 

I would like to do something like:

select id_num, name, balance  from listOfPeople left join  (   SELECT id_num, SUM(discount)   FROM calculatePersonalDiscount(listOfPeople.id_num) ) x ON x.id_num = listOfPeople.id_num 

But you can't pass listOfPeople.id_num into the function since it's not really the same scope.

like image 929
kd7iwp Avatar asked Mar 30 '09 22:03

kd7iwp


People also ask

How do you join a table with a table valued function in SQL?

SQL Server does have a solution for this called CROSS APPLY. If you use CROSS APPLY for INNER JOINS and OUTER APPLY for LEFT OUTER JOINS, then you have the ability to create a join between two table valued expressions, which in my case is a TABLE VARIABLE and the results of a TABLE VALUED FUNCTION.

Can we join table and function in SQL?

Different Types of SQL JOINs(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

How do you call a table valued function in SQL Select statement?

Description. The simple definition of the table-valued function (TVF) can be made such like that; a user-defined function that returns a table data type and also it can accept parameters. TVFs can be used after the FROM clause in the SELECT statements so that we can use them just like a table in the queries.

Can we join a function and a table?

SQL Server supports table valued functions, what are functions that return data in the form of tables. JOIN operations in SQL Server are used to join two or more tables. However, JOIN operations cannot be used to join a table with the output of a table valued function. APPLY operators are used for this purpose.


1 Answers

In SQL Server 2005 you can use the CROSS APPLY syntax:

select id_num, name, balance, SUM(x.discount) from listOfPeople     cross apply dbo.calculatePersonalDiscount(listOfPeople.id_num) x 

Likewise there's an OUTER APPLY syntax for the equivalent of a LEFT OUTER join.

like image 182
Matt Hamilton Avatar answered Sep 16 '22 17:09

Matt Hamilton