2011-09-30 83 views
10

我有一个CLR UDT会从表值方法大大受益,ALA xml.nodes()是否可以在SQL CLR用户定义类型中创建表值*方法*?

-- 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), (1, 6)....(1, 20) 
declare @udt dbo.FancyType = '1.4:20' 
select * from @udt.AsTable() t (c) 

没有人有任何经验W¯¯/这个?任何帮助将不胜感激。我尝试了一些东西,但都失败了。我查找过文档和例子,但没有找到。

是的,我知道我可以创建将我的UDT作为参数的表值UDF,但我希望将所有内容都捆绑到一种类型的OO风格中。

编辑

罗素哈特发现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中,我们得到一个运行时错误预期(如果不是字对字):

-- 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对象的限制,它可能不是明智的。这就是说,如果有人知道一个骇客来解决这个特定的限制,我会相应地提出一个新的赏金。

回答

6

你有别名但不是列。尝试,

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,这应该失败就有关不正确类型的另一个运行时错误。

的SqlMethodAttribute类从SqlFunctionAttribute类继承,所以SqlMethodAttribute继承SqlFunctionAttribute的FillRowMethodName和TableDefinition字段。这意味着可以编写一个表值方法,但情况并非如此。该方法编译和程序集部署,但有关IEnumerable返回类型的错误在运行时引发,并显示以下消息:“程序集”类中的“方法,属性或字段”具有无效的返回类型。“

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

xml节点方法不会更改,因此它不受同一实施限制的约束。

希望这有助于彼得和好运。

+0

感谢拉斯,是的,它确实有帮助。 –

相关问题