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
kay
kay
SSC Veteran
SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)

Group: General Forum Members
Points: 215 Visits: 90

Dear Forum,

I hope someone knows the trick. In a rather complicated computation, I use a table-valued function with two Parameters to compute some data. Things run fine as long as you call the function for fixed Parameters, e.g. select * from dbo.fn_myfunc(100, 200).

However, I need to run the function for all entries in a table, and this does not seem to work easily. Here's the example (simplified):

CREATE FUNCTION LargeOrderShippers ( @Freight_min money, @Freight_max money)
RETURNS @OrderShipperTab TABLE
( ShipperName nvarchar(80),
OrderID int,
Freight money
)
AS
BEGIN
INSERT @OrderShipperTab
SELECT S.CompanyName, O.OrderID, O.Freight
FROM Shippers AS S
INNER JOIN Orders AS O ON (S.ShipperID = O.ShipVia)
WHERE O.Freight between @Freight_min and @Freight_max
RETURN
END

go
create table freight_manager(manager_name varchar(20), min_value money, max_value money)
go
insert freight_manager values ('Kathy', 100, 150)
insert freight_manager values ('Fred', 150, 200)
insert freight_manager values ('Rob', 200, 300)
insert freight_manager values ('Ken', 300, 700)

Now, how do you call the function for each table-entry ? A cross-join does not seem to work....

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

...thorws a sytnax error. Any ideas? I can't believe you need a cursor to step through the table

Regards,

Kay





Mike C
Mike C
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1351 Visits: 1168
Believe it Seems intuitive, doesn't it? But of course it's not. This is one of the issues fixed in SQL 2005 with the censored. For the example you gave, you could use a VIEW instead ofa UDF. Of course if you want to add additional processing logic that won't work.
SwePeso
SwePeso
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2469 Visits: 3431

For the first, CROSS JOIN creates a CARTESIAN PRODUCT, which means you end up with ALL combinations of freight_manager and LargeOrderShippers!

Alter your function to include something to bind freight_manager to shippers and order.

Then call with either

select *
from freight_manager
inner join dbo.LargeOrderShippers(min_value, max_value) AS los on los.someID = freight_manager.someID




N 56°04'39.16"
E 12°55'05.25"
David A. Long
David A. Long
SSChasing Mays
SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)

Group: General Forum Members
Points: 643 Visits: 237

Per Books Online (BOL):

"Only constants and @local_variables can be passed to table-valued functions."

"The value of each declared parameter must be supplied by the user when the function is executed."

This means that the parameters (@Freight_min money, @Freight_max money) can be a variable or a constant, but cannot be a per row value as the parameters are evaluated when executed.

Example:

SELECT * FROM LargeOrderShippers(500,1000)

DECLARE @Freight_min money, @Freight_max money
SELECT @Freight_min money = 500, @Freight_max money = 1000
SELECT * FROM LargeOrderShippers(@Freight_min, @Freight_max)

Andy





Mike C
Mike C
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1351 Visits: 1168
Did you actually run this in QA? You can't passcolumn names as parametersto a TVF in SQL 2000! In SQL 2005 you can do it via the censored operator.
Nestor Jarquin
Nestor Jarquin
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 137
Hi!
Can I ask how to do that???
I want to pass a column name as parameter to a function from within a select statement. Something interesting is that my function is a tvf... Meaning that I would have to use a second SELECT in order to specify what field I want to retrieve from the function. Here's an example:
SELECT fname, lname, SELECT totalincome FROM dbo.CalculateIncome(PersonID) as Income FROM Persons

This is just an example... I can do this with UDF that return a scalar value, I suppose it's because I don't have to use another select statement...

Is this really posible???

Thanks in advance.
Mike C
Mike C
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1351 Visits: 1168
Something like this should do the trick, using censored:

Sample code removed at Segiy's Request
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5846 Visits: 11406
Mike C (10/15/2007)
Something like this should do the trick, using CROSS APPLY:

Yes, but not in SQL2000.

Table function approach is just wrong for this.
You need a value per line of Persons table. So, you need a scalar function.

If you want to stick with table approach use a view.
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5846 Visits: 11406
CREATE VIEW LargeOrderShippers
AS
SELECT S.CompanyName, O.OrderID, O.Freight
FROM Shippers AS S
INNER JOIN Orders AS O ON (S.ShipperID = O.ShipVia)
GO

select S.*
from freight_manager M
INNER JOIN LargeOrderShippers S ON O.Freight >=M.min_value AND O.Freight <=M.max_value
Nestor Jarquin
Nestor Jarquin
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 137
Mike C (10/15/2007)
Something like this should do the trick, using CROSS APPLY:

SELECT *
FROM Persons p
CROSS APPLY dbo.CalculateIncome(p.personid);


Thanks for your help guys!!! I tried all sorts of stuff and couldn't make it work, so I ended up transforming my table valued function into a scalar function...
I'm a bit curious though, if I use the statement you wrote I will retrieve each and every column from the function (remember the function returns a table variable with more than one column). So how should I specify the column name that I want to retrieve...

Like I said, I solved the issue. It's just curiosity. Wink

Thanks!
JarZe
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