SQL Server 2008:多语句 UDF 能否返回 UDT?

SQL Server 2008: Can a multi-statement UDF return a UDT?(SQL Server 2008:多语句 UDF 能否返回 UDT?)
本文介绍了SQL Server 2008:多语句 UDF 能否返回 UDT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

多语句 UDF 是否有可能返回用户定义的表类型,而不是在其返回参数中定义的表?

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?

所以代替:

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

我想做:

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

这样做的原因是我的函数内部调用了其他函数,必须传入MyTableType UDT,即使我在RETURN表类型中定义了完全相同的表定义,也会抛出操作数冲突错误.

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.

推荐答案

我能想到的最好办法是声明一个您的类型的表变量本地函数,并在整个代码中使用它.然后在 RETURN 语句之前对参数表执行 INSERT...SELECT 操作.

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.

这篇关于SQL Server 2008:多语句 UDF 能否返回 UDT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SQL query to group by day(按天分组的 SQL 查询)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
sql group by versus distinct(sql group by 与不同)
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)
Count number of records returned by group by(统计分组返回的记录数)
SQL GROUP BY CASE statement with aggregate function(带聚合函数的 SQL GROUP BY CASE 语句)