如果您创建一个C#类是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
namespace ExtractMdxParts
{
public partial class UserDefinedFunctions
{
public class EventData
{
public SqlString Product;
public SqlString CategoryFilter;
public SqlString Group;
public SqlString ExtraData;
}
[Microsoft.SqlServer.Server.SqlFunction(
FillRowMethodName = "FillRow",
TableDefinition = "Product nvarchar(128), CategoryFilter nvarchar(128), Group nvarchar(128), Extradata nvarchar(MAX)",
IsDeterministic = true)]
public static IEnumerable ExtractParts([SqlFacet(MaxSize = -1)] String MdxString)
{
string[] parts = MdxString.Split(".".ToCharArray(), 4, StringSplitOptions.None);
if (parts.Length < 3)
{
return null;
}
List<EventData> x = new List<EventData> { };
char[] trimChars = "[]".ToCharArray();
EventData y = new EventData { Product = parts[0].Trim(trimChars), CategoryFilter = parts[1].Trim(trimChars), Group = parts[2].Trim(trimChars) };
if (parts.Length == 4)
{
y.ExtraData = string.Join(",", parts[3].Split(".".ToCharArray()).Select(p => p.Substring(1).Trim(trimChars)));
}
x.Add(y);
return x;
}
public static void FillRow(object eventData, out SqlString product, out SqlString categoryFilter, out SqlString group, out SqlString extraData)
{
//I'm using here the EventData class defined above
EventData ed = (EventData)eventData;
product = new SqlString(ed.Product.ToString());
categoryFilter = new SqlString(ed.CategoryFilter.ToString());
group = new SqlString(ed.Group.ToString());
extraData = new SqlString(ed.ExtraData.ToString());
}
}
}
而且它与
use [testing]
go
CREATE ASSEMBLY ExtractMdxParts
FROM 'C:\Your\Path\Here\ExtractMdxParts.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
GO
CREATE FUNCTION ExtractParts (@MdxString nvarchar(MAX))
RETURNS TABLE (Product nvarchar(128),
CategoryFilter nvarchar(128),
[Group] nvarchar(128),
ExtraData nvarchar(MAX)
)
AS EXTERNAL NAME [ExtractMdxParts].[ExtractMdxParts.UserDefinedFunctions].ExtractParts
GO
其中 '测试' 是你的数据库的名称和您添加到SQL Server所有的烦杂的位允许CLR程序...
然后你可以做的事情,如
SELECT * FROM dbo.ExtractParts(N'[a].[b].[c].&[e].&[f]')
,并得到像
Product CategoryFilter Group ExtraData
-----------------------------------------
a b c e,f
重要的部分,我不得不使用,使一个表它的工作:
using System.Collections;
这样就可以public static IEnumerable ExtractParts...
new SqlString(...
可能不是重要,但我越来越绝望,因为“因为T-SQL和CLR类型...不匹配“
- 我没有去签署大会的方式,因为VS不会让我签署大会,并没有给我一个有意义的错误信息,所以我做了蛮力
ALTER AUTHORIZATION ON DATABASE::testing TO [computername\accountname]
。使用签名:我只是写这个作为对这个问题的回答;你将有更多的时间在没有邻居抱怨的情况下喊VS.
- 另一个可能不需要的东西是
[SqlFacet(MaxSize = -1)]
它出自Adam Machanic写的something。但是,如果他写它,它很可能是一个好主意。
哦......我使用的SQL Server 2014一起选择使用.NET 4.5.2我VS2015社区版。
我测试了它为具有以下数据的工作:
[] [bhgshsfhsf] [chhhhhhhhhhhhhhhhhhhh]。 & [qwert]。 & [asd]。 & [tyu]。 & []。 & vbncmzxvb] [a134141]。[bhgshsfhsf]。[chhhhhhhhhhhhhhhhhhhh]。 & [qwert]。 & [asd]。 & [tyu]。 & []。 & vbncmzxvb] [a134141] [bhgshsfhsf] [chhhhhhhhhhhhhhhhhhhh] [A]。[B] [C]
它返回NULL
为ExtraData
列,如果没有额外的数据。
如果将NULL
传递给它,则会失败,并且如果将空字符串传递给它,则会为每个列提供一个空字符串。这对你来说可能是或者可能不是问题。
参考:Introduction to SQL Server CLR table valued functions
方括号之间允许使用哪些字符? –
SQL Server Analysis Services似乎只允许使用字母数字,_, - 和空格。 – dgills