我有一个C#(.NET框架4.7.2)脚本来执行一些跟踪代码的自定义读取,我正try 使用它来创建一个SQL(V14.0.3445.2)表值函数.C#FillRow方法如下所示:
public partial class UserDefinedFunctions
{
[SqlFunction(DataAccess = DataAccessKind.Read, FillRowMethodName = "Read_Trace",
TableDefinition =
" Site NVARCHAR(3)" +
",Line TINYINT" +
",Shift NVARCHAR(2)" +
",ProductionDate DATETIME2(0)" +
",ProductionTime NVARCHAR(5)" +
",DayName NVARCHAR(9)" +
",Perfect_Trace BIT" +
",NWFGenerated_Trace BIT" +
",Altered_Trace BIT")]
public static IEnumerable Read_Trace([SqlFacet(MaxSize = 255)] SqlChars customer, [SqlFacet(MaxSize = 255)] SqlChars trace)
{
var trc = trace.ToString();
RegexList regexList = GetRegexList(customer.ToString(), trace.ToString());
return new List<TraceContents>
{
new TraceContents
{
Site = GetValue(regexList.Site_Regex, trc),
Line = SqlInt16.Parse(new string(GetValue(regexList.Line_Regex, trc).Where(char.IsNumber).ToArray())),
Shift = GetValue(regexList.Shift_Regex, trc),
ProductionDate = JulianToDate(GetValue(regexList.Date_Regex, trc)),
ProductionTime = GetValue(regexList.Time_Regex, trc),
DayName = GetValue(regexList.Day_Regex, trc),
Perfect_Trace = regexList.Perfect_Trace,
NWFGenerated_Trace = regexList.NWFGenerated_Trace,
Altered_Trace = regexList.Altered_Trace
}
};
}
我使用以下SQL脚本将其转换为函数:
DROP ASSEMBLY if exists CLR_Functions;
CREATE ASSEMBLY CLR_Functions
FROM 'C:\Temp\Functions.dll'
GO
CREATE FUNCTION fn_ReadTraceCode(@InvoiceAccount NVARCHAR(255), @Trace NVARCHAR(255)) RETURNS
TABLE (
[Site] NVARCHAR(3)
,[Line] TINYINT
,[Shift] NVARCHAR(2)
,[ProductionDate] DATETIME2(0)
,[ProductionTime] NVARCHAR(5)
,[DayName] NVARCHAR(9)
,[Perfect_Trace] BIT
,[NWFGenerated_Trace] BIT
,[Altered_Trace] BIT)
AS EXTERNAL NAME CLR_Functions.UserDefinedFunctions.Read_Trace
GO
这两个脚本的参数数量匹配,我认为这是必需的,但当我运行SQL脚本时,我得到以下错误:
消息6208,级别16,状态1,过程FN_ReadTraceCode,第2行[批次 Start Line 4]Create Function失败,因为参数计数 FillRow方法应该比 表值CLR函数.
我不知道我做错了什么--我遵循了逐步指南(如this),但没有找到任何关于需要额外参数的东西,事实上,在此之前我曾经做过一个CLR函数,似乎不需要额外的参数……网上对这个问题的唯一答案是,在一次事件中,该函数的return列之间存在真正的不匹配,这加剧了我的困惑.
你能帮我停止这个错误的出现,这样我的函数就可以编译了吗?如果需要,我可以发布完整的代码,但我认为问题出在上面列出的FillRow方法上.