sql-server – 是否可以在SQL CLR用户定义的类型中创建表值*方法*?

我有一个CLR UDT,它将从表值方法ala xml.nodes()中受益匪浅:
-- nodes() example,for reference:
declare @xml xml = '<id>1</id><id>2</id><id>5</id><id>10</id>'
select c.value('.','int') as id from @xml.nodes('/id') t (c)

我想为我的UDT类似的东西:

-- would return tuples (1,4),(1,5),6)....(1,20)
declare @udt dbo.FancyType = '1.4:20'
select * from @udt.AsTable() t (c)

有人有任何经验吗?任何帮助将不胜感激.我已经尝试了一些事情,他们都失败了.我已经找到了文档和示例,没有找到.

是的,我知道我可以创建使用UDT作为参数的表值UDF,但我希望将所有内容捆绑在单一类型的OO样式中.

编辑

Russell Hart发现了the documentation states that table-valued methods are not supported,并修复了我的语法以产生预期的运行时错误(见下文).

在VS2010中,在创建了一个新的UDT之后,我在结构定义的末尾添加了这个:

[sqlMethod(FillRowMethodName = "GetTable_FillRow",TableDeFinition = "Id INT")]
public IEnumerable GetTable()
{
    ArrayList resultCollection = new ArrayList();
    resultCollection.Add(1);
    resultCollection.Add(2);
    resultCollection.Add(3);
    return resultCollection;
}

public static void GetTable_FillRow(object tableResultObj,out sqlInt32 Id)
{
    Id = (int)tableResultObj;
}

这样可以成功构建和部署.但是在SSMS中,我们会按预期的方式得到一个运行时错误(如果不是word-for-word):

-- needed to alias the column in the SELECT clause,rather than after the table alias.
declare @this dbo.tvm_example = ''
select t.[Id] as [ID] from @this.GetTable() as [t]

Msg 2715,Level 16,State 3,Line 2
Column,parameter,or variable #1: Cannot find data type dbo.tvm_example.
Parameter or variable '@this' has an invalid data type.

所以,似乎这是不可能的.即使有可能,由于在sql Server中更改CLR对象的限制,这可能不是明智的.

也就是说,如果有人知道要克服这个特殊的限制,我会相应地提出一个新的赏金.

解决方法

您已经将列表别名,而不是列.尝试,
declare @this dbo.tvm_example = ''
select t.[Id] as [ID] from @this.GetTable() as [t]

根据文档http://msdn.microsoft.com/en-us/library/ms131069(v=SQL.100).aspx#Y4739,对于不正确的类型,这应该会导致另一个运行时错误.

The sqlMethodAttribute class inherits from the sqlFunctionAttribute class,so sqlMethodAttribute inherits the FillRowMethodName and TableDeFinition fields from sqlFunctionAttribute. This implies that it is possible to write a table-valued method,which is not the case. The method compiles and the assembly deploys,but an error about the IEnumerable return type is raised at runtime with the following message: “Method,property,or field ” in class ” in assembly ” has invalid return type.”

他们可能避免支持这种方法.如果使用方法更新更改程序集,则可能会导致UDT列中的数据出现问题.
一个适当的解决方案是具有最小的UDT,然后是一个单独的方法来配合它.这将确保灵活性和功能齐全的方法.

xml节点方法不会改变,所以它不会受到相同的实现限制.

希望这能帮助彼得,祝你好运.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...