How to join a table-valued Function

  • 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

     

  • 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.

  • 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"

  • 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

     

  • 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.

  • 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.

  • Something like this should do the trick, using [font="Courier New"]censored[/font]:

    Sample code removed at Segiy's Request

  • Mike C (10/15/2007)


    Something like this should do the trick, using [font="Courier New"]CROSS APPLY[/font]:

    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.

    _____________
    Code for TallyGenerator

  • 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

    _____________
    Code for TallyGenerator

  • Mike C (10/15/2007)


    Something like this should do the trick, using [font="Courier New"]CROSS APPLY[/font]:

    [font="Courier New"]SELECT *

    FROM Persons p

    CROSS APPLY dbo.CalculateIncome(p.personid);[/font]

    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

  • Example removed at Sergiy's request.

  • Mike,

    unfortunately there is no such thing as "CROSS APPLY" in MS SQL 2000.

    _____________
    Code for TallyGenerator

  • Thanks a lot for your time and help....

    I'll give it a try.

  • Sergiy,

    Would it make you feel better if I said this feature was introduced in SQL 2005, as I did a couple of times in the first couple of posts of this entire thread?

  • Mike, it would be nice if you'd pay some attention to the name of the forum.

    It's on top of the page, between "Home" and "T-SQL".

    There is no point to repeatedly post SQL2005 solution not appropriate to SQL2000 on this forum.

    _____________
    Code for TallyGenerator

Viewing 15 posts - 1 through 15 (of 29 total)

You must be logged in to reply to this topic. Login to reply