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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Error casting varchar containing a number expressed in scientific notation to decimal(将包含以科学记数法表示的数字的 varchar 转换为十进制时出错)
Calculate cumulative product value(计算累积产品价值)
SQL Server loop for changing multiple values of different users(用于更改不同用户的多个值的 SQL Server 循环)
Merge columns in sql(合并sql中的列)
SQL Column is invalid in ORDER BY, not contained in aggregate or GROUP BY(SQL 列在 ORDER BY 中无效,未包含在聚合或 GROUP BY 中)
T-SQL : UNION ALL view not updatable because a partitioning column was not found(T-SQL:UNION ALL 视图不可更新,因为未找到分区列)