Sergej Andrejev
Sergej Andrejev

Reputation: 9403

Returning table-type by function

Can user-defined table-valued (or any other function) return table-type in SQL Server 2008?

Upvotes: 4

Views: 6915

Answers (1)

Aaronaught
Aaronaught

Reputation: 122624

At present, it does not seem to be possible to return a UDTT from a UDF. A UDF can return a table variable, or an inline table.

Returning an inline table:

CREATE FUNCTION dbo.MyFunc1
RETURNS TABLE
AS RETURN

SELECT <columns>
FROM <table>
WHERE <conditions>

Returning a table variable:

CREATE FUNCTION dbo.MyFunc2
RETURNS @Tbl TABLE
(
    ID int,
    Name varchar(50)
)
AS BEGIN
    INSERT @Tbl (ID, Name)
        SELECT ID, Name
        FROM <table>
        WHERE <conditions>
    RETURN
END

Those are the only types of TVFs at the moment.

Upvotes: 4

Related Questions