Passing Type Table as parameters into Function

  • Hi All,

    I am having an experiment with passing tables as parameters into Functions. I have a table type set-up as follows

    CREATE TYPE MainProd AS TABLE

    (

    Year VARCHAR(4)

    ,ID 1 INT

    ,ID 2 INT

    ,hours INT

    )

    GO

    I have a function that uses the MainProd as a read only type.

    When I try to use the function I find I have to first declare a variable with this type, an example

    DECLARE @P MainProd

    Then call the function in SQL using the @P variable after inserting table values into @P

    SELECT * FROM [dbo].[MyBestProd] (@P) AS X

    Can I simply not write the SQL in the function parameter instead of having to use the @P parameter?

    I tried

    SELECT * FROM [dbo].[MyBestProd] ([my sql statement here]) AS X

    but it did not like it.

    Thanks

    Eliza

  • I agree that passing in a subquery for the TYPEd table instead of having to predefine the table would be nice but as you have ascertained, SQL doesn't support it (at least not SQL 2008).


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 2 posts - 1 through 1 (of 1 total)

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