SQL Server 2008: Can a multi-statement UDF return a UDT? [duplicate]

谁说胖子不能爱 提交于 2020-01-04 05:46:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!