是否可以在 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), (1, 6)....(1, 20)
declare @udt dbo.FancyType = '1.4:20'
select * from @udt.AsTable() t (c)
有人有任何经验吗? / 这?任何帮助将不胜感激。我尝试了一些方法,但都失败了。我查找了文档和示例,但没有找到。
是的,我知道我可以创建以 UDT 作为参数的表值 UDF,但我更希望将所有内容捆绑在单一类型、OO 风格中。
编辑
Russell Hart 发现 文档指出不支持表值方法,并修复了我的语法以产生预期的运行时错误(见下文)。
在 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 对象的限制,也可能不是明智之举。
也就是说,如果有人知道解决这个特定限制的方法,我将相应地提高新的赏金。
I have a CLR UDT that would benefit greatly from table-valued methods, 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)
I want something similar for my 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)
Does anyone have any experience w/ this? Any help would be greatly appreciated. I've tried a few things and they've all failed. I've looked for documentation and examples and found none.
Yes, I know I could create table-valued UDFs that take my UDT as a parameter, but I was rather hoping to bundle everything inside a single type, OO-style.
EDIT
Russell Hart found the documentation states that table-valued methods are not supported, and fixed my syntax to produce the expected runtime error (see below).
In VS2010, after creating a new UDT, I added this at the end of the struct definition:
[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;
}
This builds and deploys successfully. But then in SSMS, we get a runtime error as expected (if not 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.
So, it seems it is not possible after all. And even if were possible, it probably wouldn't be wise, given the restrictions on altering CLR objects in SQL Server.
That said, if anyone knows a hack to get around this particular limitation, I'll raise a new bounty accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经为表设置了别名,但没有为列设置了别名。尝试,
根据文档, http:// /msdn.microsoft.com/en-us/library/ms131069(v=SQL.100).aspx#Y4739,这应该会因另一个有关不正确的运行时错误而失败 类型。
他们可能会避免支持这种方法。如果您通过方法更新更改程序集,可能会导致 UDT 列中的数据出现问题。
适当的解决方案是使用最小的 UDT,然后使用一类单独的方法来配合它。这将确保方法的灵活性和功能齐全。
xml 节点方法不会改变,因此它不受相同的实现限制。
希望这对彼得有帮助,祝你好运。
You have aliased the table but not the columns. Try,
According to the documentation, http://msdn.microsoft.com/en-us/library/ms131069(v=SQL.100).aspx#Y4739, this should fail on another runtime error regarding incorrect type.
They may be avoiding supporting such a method. If you alter the assembly with method updates this can cause problems to the data in UDT columns.
An appropriate solution is to have a minimal UDT, then a seperate class of methods to accompany it. This will ensure flexibility and fully featured methods.
xml nodes method will not change so it is not subject to the same implemetation limitations.
Hope this helps Peter and good luck.