Home Forums SQL Server 2005 Development Accessing data returned from a table valued function RE: Accessing data returned from a table valued function

  • popq79 34287 (12/13/2012)


    Hi All,

    Is there a way to access values return from a table-valued function?

    Sample code:

    CREATE FUNCTION [dbo].[fn_MyFunction]

    (@ID INT)

    RETURNS @return_table TABLE

    (Col1 int NULL,

    Col2 int NULL)

    AS

    --- processing goes here...

    INSERT @return_table

    SELECT @col1_newvalue,@col2_newvalue

    RETURN

    Need to call above function from stored procedure as follows;

    CREATE procedure MyProc

    AS

    SELECT col1, col2 from dbo.fn_MyFunction(1)

    -- want to store return values for further processing by assigning them to local variables

    declare @newvalue1 int

    declare @newvalue2 int

    set @newvalue1 = col1

    set @newvalue2 = col2

    --- start processing @newvalue1 and @newvalue2 further

    Is this possible in SQL 2005?

    Thanks in advance.

    Check under CREATE FUNCTION in Books Online. If you can write the code as a single query, the you can make this function an "INLINE" Table Valued function which will be MUCH faster than a "MULTI-LINE" Table Valued function, which is what you have right now.

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)