2010-08-10 29 views
3

我正在嘗試創建一個UDF,根據時間做兩件不同的事情。以下是我的代碼。我想知道是否可以在UDF中使用IF語句,因爲我正在設置4個錯誤,在Begin和Return附近的語法不正確,並且返回值的返回值也無法在此上下文中使用....任何建議?SQL你可以在UDF中使用IF語句嗎?你如何做到這一點?

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 

回答

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

校正時進行

移動,返回開始之前

@ FinalMinutes1的IF之外

感動聲明塊

刪除不必要的括號

作出返回最後聲明的功能

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 
相關問題