我有一個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對象的限制,它可能不是明智的。這就是說,如果有人知道一個駭客來解決這個特定的限制,我會相應地提出一個新的賞金。
感謝拉斯,是的,它確實有幫助。 –