Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to join a table-valued Function


How to join a table-valued Function

Author
Message
J-440512
J-440512
SSC-Addicted
SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)

Group: General Forum Members
Points: 447 Visits: 949
Matt,

I stand corrected. Thank you.

I have not made the switch to 2K5 and do not plan to, if it is at all possible to jump directly from 2K to 2K8.

Regards
lalit.madan
lalit.madan
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 32
/*this behaves like inner join*/

select * from freight_manager
cross apply LargeOrderShippers ( freight_manager.min_value, freight_manager.max_value)


/*this behaves like cross join*/

select * from freight_manager
outer apply LargeOrderShippers ( freight_manager.min_value, freight_manager.max_value)

:-)
awadhesh.singh
awadhesh.singh
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 40
Mike answer is perfect answer . When you want to use join between a table and table valued function you can choose either outer apply or cross apply as per your requirement.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19016
lalit.madan (7/19/2011)
/*this behaves like inner join*/

select * from freight_manager
cross apply LargeOrderShippers ( freight_manager.min_value, freight_manager.max_value)


/*this behaves like cross join*/

select * from freight_manager
outer apply LargeOrderShippers ( freight_manager.min_value, freight_manager.max_value)

:-)


I think you will find OUTER APPLY behaves more like LEFT (OUTER) JOIN.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
sanajmshaji
sanajmshaji
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 22
Hi Guys
Please refer the below link.
http://www.java2s.com/Tutorial/SQLServer/0080__Table-Join/UsingAPPLYtoInvokeaTableValuedFunctionforEachRow.htm

Step 1
---------------------------------------------------------------------
CREATE FUNCTION dbo.fn_getByID(@ID int) RETURNS TABLE
AS
RETURN SELECT first_name FROM employee WHERE ID = @ID
GO
---------------------------------------------------------------------
Step 2
SELECT w.last_name FROM employee w
CROSS APPLY dbo.fn_getByID
(w.ID) AS r
ORDER BY w.ID;
======================================
GO
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
sanajmshaji (11/9/2011)
Hi Guys
Please refer the below link.
http://www.java2s.com/Tutorial/SQLServer/0080__Table-Join/UsingAPPLYtoInvokeaTableValuedFunctionforEachRow.htm

Step 1
---------------------------------------------------------------------
CREATE FUNCTION dbo.fn_getByID(@ID int) RETURNS TABLE
AS
RETURN SELECT first_name FROM employee WHERE ID = @ID
GO
---------------------------------------------------------------------
Step 2
SELECT w.last_name FROM employee w
CROSS APPLY dbo.fn_getByID
(w.ID) AS r
ORDER BY w.ID;
======================================
GO


That won't work in sql2000. There is no CROSS APPLY yet. :-D

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
fermin.js
fermin.js
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 0
Hi
This would also work

SELECT *
FROM dbo.CalculateIncome(p.personid);

Fermön
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44996 Visits: 39880
fermin.js (11/29/2011)
Hi
This would also work

SELECT *
FROM dbo.CalculateIncome(p.personid);

Fermön


I know this post is nearly a year old but ran across it just now. My question is, where does "p.personid" come from in that query?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
juan.wilke
juan.wilke
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 25
Great post. Thank you!
It works like a dream......
Ken McCreery
Ken McCreery
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 10
I believe what you want is the CROSS APPLY function: https://technet.microsoft.com/en-US/library/ms175156(v=SQL.105).aspx
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search