如果您創建一個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