SQL可以在UDF中使用IF语句吗?你是如何做到的?

时间:2010-08-10 15:44:57

标签: sql sql-server sql-server-2005

我正在尝试创建一个UDF,它根据时间执行两个不同的操作。以下是我的代码。我想知道你是否可以在UDF中使用IF语句,因为我正在编写4个错误,在Begin和Returns附近的语法不正确,并且返回值的返回语句也不能在此上下文中使用....任何建议?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_TEST]
(
    @StartDate DATETIME,
    @EndDate DATETIME
)

--DECLARE @StartDate DATETIME
--DECLARE @EndDate DATETIME
--SET @StartDate = '2010-07-06 14:46:37.577' 
--SET @EndDate = '2010-07-09 09:04:31.290'
BEGIN
RETURNS VARCHAR(MAX)

(
IF (CONVERT(VARCHAR(13), @StartDate, 114) > CONVERT(VARCHAR(13), @EndDate, 114))
BEGIN
DECLARE @NonWorkTime1 INT
SET @NonWorkTime1 = 780
--How many minutes are between order start and end time including non working time
DECLARE @AllMins1 INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay1 DECIMAL
SET @MinsInDay1 = 1440.0
--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 
--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays1 INT
SET @MinDays1 = (@AllMins1/@MinsInDay1)
--Subtracts complete day non worked minutes from final minutes between orders
DECLARE @FinalMinutes1 AS DECIMAL
SET @FinalMinutes1 = (@AllMins1 - (@MinDays1 * @NonWorkTime1) - 360 - 420)
RETURN @FinalMinutes1
END
ELSE
BEGIN
--RETURNS VARCHAR(MAX)
--How many minutes a day are not worked for trips
DECLARE @NonWorkTime INT
SET @NonWorkTime = 780
--How many minutes are between order start and end time including non working time
DECLARE @AllMins INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay DECIMAL
SET @MinsInDay = 1440.0
--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 
--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays INT
SET @MinDays = (@AllMins/@MinsInDay)
--Subtracts complete day non worked minutes from final minutes between orders
DECLARE @FinalMinutes AS DECIMAL
SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime))
RETURN @FinalMinutes
END
)
END

2 个答案:

答案 0 :(得分:3)

你绝对可以在UDF中使用IF。 有一些语法错误,但主要问题是您需要将@FinalMinutes移动到IF之外,因为它需要从主范围返回。

试试这个:

CREATE FUNCTION [dbo].[udf_TEST] 
( 
    @StartDate DATETIME, 
    @EndDate DATETIME 
) 
RETURNS VARCHAR(MAX) 

--DECLARE @StartDate DATETIME 
--DECLARE @EndDate DATETIME 
--SET @StartDate = '2010-07-06 14:46:37.577'  
--SET @EndDate = '2010-07-09 09:04:31.290' 
BEGIN 
DECLARE @FinalMinutes AS DECIMAL 
IF (CONVERT(VARCHAR(13), @StartDate, 114) > CONVERT(VARCHAR(13), @EndDate, 114)) 
BEGIN 
DECLARE @NonWorkTime1 INT 
SET @NonWorkTime1 = 780 
--How many minutes are between order start and end time including non working time 
DECLARE @AllMins1 INT  
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided 
DECLARE @MinsInDay1 DECIMAL 
SET @MinsInDay1 = 1440.0 
--Finds how many minutes are between start and end time excluding weekends and assignes to variable  
SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate))  
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)   
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60) 
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))  
--Calculates how many days have elapsed in the minutes that the order has taken 
DECLARE @MinDays1 INT 
SET @MinDays1 = (@AllMins1/@MinsInDay1) 
--Subtracts complete day non worked minutes from final minutes between orders 
SET @FinalMinutes = (@AllMins1 - (@MinDays1 * @NonWorkTime1) - 360 - 420) 
END 
ELSE 
BEGIN 
--RETURNS VARCHAR(MAX) 
--How many minutes a day are not worked for trips 
DECLARE @NonWorkTime INT 
SET @NonWorkTime = 780 
--How many minutes are between order start and end time including non working time 
DECLARE @AllMins INT  
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided 
DECLARE @MinsInDay DECIMAL 
SET @MinsInDay = 1440.0 
--Finds how many minutes are between start and end time excluding weekends and assignes to variable  
SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate))  
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)   
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60) 
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))  
--Calculates how many days have elapsed in the minutes that the order has taken 
DECLARE @MinDays INT 
SET @MinDays = (@AllMins/@MinsInDay) 
--Subtracts complete day non worked minutes from final minutes between orders 
SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime)) 
END
RETURN @FinalMinutes
END 

答案 1 :(得分:1)

更正

在BEGIN之前移动了RETURNS

将@ FinalMinutes1的声明移到IF块之外

删除了不需要的parens

制作RETURN最后一个功能声明

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_TEST]
(
    @StartDate DATETIME,
    @EndDate DATETIME
)

--DECLARE @StartDate DATETIME
--DECLARE @EndDate DATETIME
--SET @StartDate = '2010-07-06 14:46:37.577' 
--SET @EndDate = '2010-07-09 09:04:31.290'
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @FinalMinutes1 AS DECIMAL

IF (CONVERT(VARCHAR(13), @StartDate, 114) > CONVERT(VARCHAR(13), @EndDate, 114))
BEGIN
DECLARE @NonWorkTime1 INT
SET @NonWorkTime1 = 780
--How many minutes are between order start and end time including non working time
DECLARE @AllMins1 INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay1 DECIMAL
SET @MinsInDay1 = 1440.0
--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 
--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays1 INT
SET @MinDays1 = (@AllMins1/@MinsInDay1)
--Subtracts complete day non worked minutes from final minutes between orders

SET @FinalMinutes1 = (@AllMins1 - (@MinDays1 * @NonWorkTime1) - 360 - 420)
RETURN @FinalMinutes1
END
ELSE
BEGIN
--RETURNS VARCHAR(MAX)
--How many minutes a day are not worked for trips
DECLARE @NonWorkTime INT
SET @NonWorkTime = 780
--How many minutes are between order start and end time including non working time
DECLARE @AllMins INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay DECIMAL
SET @MinsInDay = 1440.0
--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 
--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays INT
SET @MinDays = (@AllMins/@MinsInDay)
--Subtracts complete day non worked minutes from final minutes between orders
DECLARE @FinalMinutes AS DECIMAL
SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime))

END
RETURN @FinalMinutes
END