SQL Server 2008 - How do i return a User-Defined Table Type from a Table-Valued Function?

后端 未结 4 1214
星月不相逢
星月不相逢 2020-11-29 10:14

Here\'s my user-defined table type...

CREATE TYPE [dbo].[FooType] AS TABLE(
 [Bar] [INT],
)

This is what ive had to do in my table-valued f

4条回答
  •  清歌不尽
    2020-11-29 10:27

    Even though you can not return the UDTT from a function, you can return a table variable and receive it in a UDTT as long as the schema match. The following code is tested in SQL Server 2008 R2

    -- Create the UDTT

    CREATE TYPE dbo.MyCustomUDDT AS TABLE
    (
        FieldOne varchar (512),
        FieldTwo varchar(1024)
    )
    

    -- Declare your variables

    DECLARE @uddt MyCustomUDDT;
    DECLARE @Modifieduddt MyCustomUDDT;
    

    // Call the function

    INSERT INTO @Modifieduddt SELECT * FROM dbo.MyUDF(@uddt);
    

    Function signature

    CREATE FUNCTION dbo.MyUDF(@localUDDT MyCustomUDDT)
    RETURNS @tableVar TABLE
    (
        FieldOne varchar (512),
        FieldTwo varchar(1024)
    )
    AS
    BEGIN
     --Modify your variable here
    RETURN
    END
    

    Hopefully this will help somebody.

提交回复
热议问题