在SELECT语句中使用UDF

时间:2012-06-25 01:54:29

标签: sql-server tsql user-defined-functions

我为营业时间计算制作了一个用户定义函数。

这是我的UDF。

CREATE FUNCTION  fn_GetBusinessHour (@date datetime, @addHours int)
RETURNS datetime
AS
BEGIN 
    DECLARE @CalcuatedDate datetime;
    DECLARE @addDayCount int, @addHourCount int, @addMinCount int;

    SET @addDayCount = @addHours / 8.5;
    SET @addHourCount = @addHours - (@addDayCount * 8.5);
    SET @addMinCount =  @addHours - (@addDayCount * 8.5) - @addHourCount;

    IF(@addDayCount != 0) 
        SET @CalcuatedDate = DATEADD(DD, @addDayCount, @date); 

    SET @CalcuatedDate = DATEADD(HH, @addHourCount, @CalcuatedDate);

    IF(@addMinCount != 0) 
        SET @CalcuatedDate = DATEADD(MM, @addMinCount, @CalcuatedDate); 

    RETURN @CalcuatedDate;
END

当我使用以下语句进行测试时,

SELECT dbo.fn_GetBusinessHour(GETDATE(), 40)

显示正确的结果。

但是,我使用我的功能,

SELECT TicketID
     , DateTimeLogged --Type: Datetime
     , Priority       --Type: int
     , [dbo].[fn_GetBusinessHour](DateTimeLogged, Priority)
  FROM TicketHeader

结果只显示NULL值。

TicketID    DateTimeLogged  Priority    (No column name)
1   2011-07-04 11:26:19.510     30  NULL
2   2011-07-04 13:58:45.683     30  NULL
3   2011-07-05 10:09:16.923     10  NULL
4   2011-07-05 13:13:30.237     30  NULL
5   2011-07-05 16:50:34.033     20  NULL

我尝试了CONVERT,因为它在我给出值40时起作用,但它也显示空值。

SELECT TicketID
         , DateTimeLogged --Type: Datetime
         , Priority       --Type: int
         , [dbo].[fn_GetBusinessHour](DateTimeLogged, CONVERT(int, Priority))
      FROM TicketHeader

如何修复此问题以使用我的UDF? 为什么会发生这件事? 我无法理解Priority和40之间的区别。

提前谢谢。

1 个答案:

答案 0 :(得分:3)

对于优先级值> 8.5,这似乎对我很好:

DECLARE @t TABLE(TicketID INT, DateTImeLogged DATETIME, Priority INT);

INSERT @t SELECT 1,'20110704 11:26:19.510',30
UNION ALL SELECT 2,'20110704 13:58:45.683',30
UNION ALL SELECT 3,'20110705 10:09:16.923',10
UNION ALL SELECT 4,'20110705 13:13:30.237',30
UNION ALL SELECT 5,'20110705 16:50:34.033',20;

SELECT TicketID
     , DateTimeLogged --Type: Datetime
     , Priority       --Type: int
     , [dbo].[fn_GetBusinessHour](DateTimeLogged, Priority)
  FROM @t;

收率:

TicketID  DateTimeLogged           Priority  (No column name)
--------  -----------------------  --------  -----------------------
1         2011-07-04 11:26:19.510  30        2011-07-07 15:26:19.510
2         2011-07-04 13:58:45.683  30        2011-07-07 17:58:45.683
3         2011-07-05 10:09:16.923  10        2011-07-06 11:09:16.923
4         2011-07-05 13:13:30.237  30        2011-07-08 17:13:30.237
5         2011-07-05 16:50:34.033  20        2011-07-07 19:50:34.033

如果我添加另一行优先级< 8.5,例如:

INSERT @t SELECT 6,'20110705 13:13:30.237',5;

然后将此行添加到结果中:

TicketID  DateTimeLogged           Priority  (No column name)
--------  -----------------------  --------  -----------------------
6         2011-07-05 13:13:30.237  5         NULL

换句话说,如果函数逻辑将@CalculatedDate取消分配,函数将输出NULL,如果@addDayCount = 0,则会发生这种情况。在函数中你说:

IF(@addDayCount != 0) 
    SET @CalcuatedDate = DATEADD(DD, @addDayCount, @date); 

由于@addDayCount是一个INT,试试这个:

DECLARE @addDayCount INT;
SET @addDayCount = 5 / 8.5;
SELECT @addDayCount;

结果:

0

因为@CalculatedDate最初没有赋值,所以下面的所有DATEADD操作都执行DATEADD(interval,number,NULL),它仍然产生NULL。

因此,您可能需要为函数中的变量使用不同的数据类型...