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

  • This works for me.

    create FUNCTION [dbo].[fn_MyFunction]

    (@ID INT)

    RETURNS @return_table TABLE

    (Col1 int NULL,

    Col2 int NULL)

    AS begin

    --- processing goes here...

    INSERT @return_table

    SELECT 1, 2 union all

    select 55, 73

    RETURN

    end

    go

    declare @var1 int, @var2 int;

    select @var1 = col1, @var2 = col2

    from dbo.fn_MyFunction(1)

    select @var1, @var2

    However the reason I suggested using a temp table is because you want to capture all the rows not just one. If you don't need multiple rows you should consider a complete rethinking of what you are doing. For that type of situation it would be better to use a stored proc with multiple output parameters instead of using a multi statement table valued function.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/