我有一个UDF
函数可以根据作为参数传入的其他字段值计算“状态”。我已将其C#
用作 sql server 项目。
不是实际的功能,而是我的问题作为一个更简单的例子:
MyUdf(int a,int b)
:
return a+b
但我想要 :
return a+b+c
,其中c
是预定义的值,作为参数从另一个表中检索,以便以后可以更改。
问题有两个:
1)如何从另一个表中加载这个值UDF
?
更重要的是……
2)如何缓存该值以供以后使用,以便不会为每一行检索它,因为这可能是一个性能问题。
我不是在寻找解决方案,而是在寻找有关如何进行的提示。
我的实际功能:
[SqlFunction]
public static string CalcBDAlert(DateTime currentTime, string status, DateTime prevMilestoneAt, DateTime nextMilestoneAt)
{
// quick process
if (status == "Finished")
{
return "4,...";
}
else if (status == "Cancelled")
{
return "5,...";
}
#region Un-Allocated
if (status == "Un-Alloc.")
{
var minPassedSinceMilestone = (currentTime - prevMilestoneAt).TotalMinutes; // milestone was start, creation of call.
if (minPassedSinceMilestone < 15)
{
return "3,...";
}
else if (minPassedSinceMilestone < 30)
{
return "2,>15 Min";
}
else if (minPassedSinceMilestone < 35)
{
return "1,>30 Min!";
}
else
{
return "0,>35 Min!!";
}
}
#endregion
#region Allocated
else if (status == "Alloc.")
{
var minPassedSinceMilestone = (currentTime - prevMilestoneAt).TotalMinutes; // milestone was allocation of engineer.
var minRemTillNextMilestone = (nextMilestoneAt - prevMilestoneAt).TotalMinutes; // next milestone is ETA, predicted arrival time.
var percentageTillNextMilestone = minPassedSinceMilestone / minRemTillNextMilestone;
if (percentageTillNextMilestone < 0.90)
{
return "3,...";
}
else if (percentageTillNextMilestone < 0.95)
{
return "2,90-95%";
}
else if (percentageTillNextMilestone < 1)
{
return "1,>95%!";
}
else
{
return "0,>100%!!";
}
}
#endregion
#region Arrived
else if (status == "Arrived")
{
var minPassedSinceMilestone = (currentTime - prevMilestoneAt).TotalMinutes; // milestone was arrival of engineer.
var minRemTillNextMilestone = (nextMilestoneAt - prevMilestoneAt).TotalMinutes; // next milestone is completion of work.
var percentageTillNextMilestone = minPassedSinceMilestone / minRemTillNextMilestone;
if (percentageTillNextMilestone < 0.90)
{
return "3,...";
}
else if (percentageTillNextMilestone < 0.95)
{
return "2,90-95%";
}
else if (percentageTillNextMilestone < 1)
{
return "1,>95%!";
}
else
{
return "0,>100%!!";
}
}
#endregion
return "-1,Unknown";
}
我想参数化和缓存百分比 (.9, .95, 1) 和分钟 (15,30,35) 等。