注:
其他還有,CLR標(biāo)量函數(shù)和聚合函數(shù)
1. C#創(chuàng)建dll
public class UserDefinedFunctions
{
[SqlFunction(
DataAccess = DataAccessKind.Read,
FillRowMethodName = "FillRow", //需要指定填充方法
IsDeterministic = true)]
public static IEnumerable Parse(SqlString str)
{
return new List<Item>
{
new Item
{
Key = "1",
Value = "2"
},
new Item
{
Key = "3",
Value = "4"
}
};
}
public class Item
{
public SqlString Key { get; set; }
public SqlString Value { get; set; }
}
public static void FillRow(object obj,
out SqlString key,
out SqlString value)
{
Item item = obj as Item;
key = item.Key;
value = item.Value;
}
};
2. Sql Server配置
--啟用CLR
EXEC sp_configure 'clr enabled', 1
RECONFIGURE
--指定數(shù)據(jù)庫(kù)
USE TestDatabase
GO
--創(chuàng)建assembly
CREATE ASSEMBLY [Parser] FROM 'D:\temp\ClassLibrary1.dll'
GO
--創(chuàng)建function
CREATE FUNCTION func_Parse(@str nvarchar(32))
RETURNS TABLE([Key] nvarchar(32),[Value] nvarchar(32))
AS EXTERNAL NAME [Parser].[UserDefinedFunctions].[Parse]
3. 調(diào)用
SELECT * FROM [dbo].[func_Parse]('123')