views:

83

answers:

1

Is it possible that a multi-statement UDF return a User Defined Table Type, instead of a table that is defined within it's return param?

So instead of:

CREATE FUNCTION MyFunc 
(
    @p1 int, @p2 char
)
RETURNS 
@SomeVar TABLE 
(
    c1 int
)
AS

I would like to do:

CREATE FUNCTION MyFunc 
(
    @p1 int, @p2 char
)
RETURNS 
@SomeVar MyTableType
AS

The reason for this is that inside my function I call other functions and have to pass in MyTableType UDT, even if I define exactly the same table definition in the RETURN table type, it will throw an operand clash error.

+1  A: 

The best that I could come up with was to declare a table variable of your type local to the function and use that throughout your code. Then do an INSERT...SELECT into the parameter table right before the RETURN statement.

I've avoided user-defined types so far. While they seem promising, with the ability to change the type in one location instead of changing data types everywhere, they just never seem to deliver when it comes to productivity and maintenance because of issues like these.

Tom H.
I also though about that work-around, but was just hoping I overlooked something and whatever I want is actually possible.I share your opinion about them, but with this particular issue I think the benefits outweigh the ugly work-around...
Gidon