我想在USQL中的TVF表值函数中使用函数后面的代码。这可能吗 ?
我不想注册程序集,因为该功能非常特定于TVF,它只是输入字符串的一些格式。
代码背后
using System;
namespace Transform
{
public class Formatter
{
private static DateTime DefaultDateTime = DateTime.Parse("1/1/2004");
public static DateTime ToDateTime(string date, string format)
{
...
}
public static DateTime? ToNullableDateTime(string date, string format)
{
...
}
}
}
表值函数使用
SELECT
rec.id,
rec.name,
Transform.Formatter.ToDateTime(rec.effectiveDate, "yyyyMMdd"),
Transform.Formatter.ToNullableDateTime(rec.expirationDate, "yyyyMMdd")
该函数正确编译。但是,当我运行它时,它会发出一个运行时错误,指出它无法识别定义了代码的命名空间的标记Transform.
目前我使用解决方法代替后面的代码。但是,如果我能理解如何使用它,那么该函数将更具可读性。
答案 0 :(得分:2)
代码隐藏不适用于TVF的原因是TVF需要在TVF机构内部拥有所有名称解析和参考。外部依赖项(如标题后面的代码)不会泄漏到函数体中。
我们添加了一个新功能(下一个发行说明将涵盖它),使您能够在TVF内指定轻量级功能变量。例如,(我使用TryParse作为例子):
CREATE FUNCTION f() RETURNS @res AS
BEGIN
DECLARE @myfunc Func<int,string> = (f) => f.ToString();
DECLARE @TryParseInt Func<string,int?> = (s) => {int i; var b = Int32.TryParse(s, out i); return b? (int?) i : null;};
@res =
SELECT @myfunc(1) AS intcol,
@TryParseInt("42") AS stringcol
FROM(
VALUES
(
1
)) AS T(x);
END;
OUTPUT f() TO "/output/f.csv" USING Outputters.Csv();
如果您希望能够重用函数变量,您应该能够将函数变量声明添加到PACKAGE,然后在表值函数中导入PACKAGE。