问题
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:
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.
来源:https://stackoverflow.com/questions/1914956/sql-server-2008-can-a-multi-statement-udf-return-a-udt