SQL Clone
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
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1677 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
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 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
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 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
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41298 Visits: 20002
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
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 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
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62341 Visits: 17954
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 Modens 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
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

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

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

Fermön
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214266 Visits: 41979
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
juan.wilke
juan.wilke
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 25
Great post. Thank you!
It works like a dream......
Ken McCreery
Ken McCreery
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 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