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