Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

How to join a table-valued Function Expand / Collapse
Author
Message
Posted Tuesday, June 13, 2006 6:55 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, December 20, 2013 5:00 AM
Points: 212, Visits: 89

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

 




Post #286989
Posted Tuesday, June 13, 2006 7:56 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135
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.
Post #287025
Posted Tuesday, June 13, 2006 8:25 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 4:11 AM
Points: 2,397, Visits: 3,416

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"
Post #287048
Posted Wednesday, June 14, 2006 3:12 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, July 3, 2014 2:36 PM
Points: 631, Visits: 236

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

 




Post #287268
Posted Wednesday, June 14, 2006 8:23 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135
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.
Post #287364
Posted Monday, October 15, 2007 5:16 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 1:15 PM
Points: 14, Visits: 100
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.
Post #411066
Posted Monday, October 15, 2007 7:31 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135
Something like this should do the trick, using censored:

Sample code removed at Segiy's Request
Post #411087
Posted Monday, October 15, 2007 7:37 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:08 PM
Points: 4,573, Visits: 8,353
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.
Post #411089
Posted Monday, October 15, 2007 7:41 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:08 PM
Points: 4,573, Visits: 8,353
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
Post #411091
Posted Tuesday, October 16, 2007 6:09 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 1:15 PM
Points: 14, Visits: 100
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. ;)

Thanks!
JarZe
Post #411519
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse