在函数中使用递归查询

时间:2018-02-13 12:45:07

标签: sql-server

我在这里的一个人的帮助下开发了这个查询,我无法在函数中使用它。 存在某种语法问题。

这是查询

WITH CTE AS (
    SELECT @STARTDATE AS STARTDATE
    UNION ALL
    SELECT DATEADD(D,1,STARTDATE) 
    FROM CTE
    WHERE STARTDATE <@ENDDATE
),
WORKINGDAYS AS (
    SELECT STARTDATE,
           DATENAME(DW,STARTDATE)WEEKDAYS,
           C1.CalanderDayName AS isweekend
    FROM CTE c
         LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE) = C1.CalanderDayName
                                      AND C1.IsOffDay = 1
)

SELECT COUNT(WEEKDAYS)as WORKINGDAYS
FROM WORKINGDAYS
WHERE isweekend IS NULL;

我想创建一个名为fnGetWorkingDays的函数

ALTER FUNCTION [dbo].[fnGetWorkingDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN
     DECLARE @dateFrom datetime 
     DECLARE @dateTo datetime 
     SET @dateFrom = @StartDate 
     SET @dateTo = @EndDate

     DECLARE @WORKDAYS INT
     SELECT @WORKDAYS =

;WITH CTE AS (
SELECT @STARTDATE  AS STARTDATE
UNION ALL
select DATEADD(D,1,STARTDATE) 
FROM CTE
WHERE STARTDATE <@ENDDATE
)
,WORKINGDAYS AS (
SELECT STARTDATE,DATENAME(DW,STARTDATE)WEEKDAYS, C1.CalanderDayName AS isweekend
FROM CTE c
LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE)=C1.CalanderDayName AND C1.IsOffDay=1
)

SELECT COUNT(WEEKDAYS)as WORKINGDAYS FROM WORKINGDAYS WHERE isweekend is null

     RETURN @WORKDAYS
END

3 个答案:

答案 0 :(得分:3)

尝试使用此下方功能。我知道任何错误

CREATE FUNCTION [dbo].[fnGetWorkingDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN 

     DECLARE @WORKDAYS INT


;WITH CTE AS (
    SELECT @STARTDATE AS STARTDATE
    UNION ALL
    SELECT DATEADD(D,1,STARTDATE) 
    FROM CTE
    WHERE STARTDATE <@ENDDATE
),
WORKINGDAYS AS (
    SELECT STARTDATE,
           DATENAME(DW,STARTDATE)WEEKDAYS,
           C1.CalanderDayName AS isweekend
    FROM CTE c
         LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE) = C1.CalanderDayName
                                      AND C1.IsOffDay = 1
)

SELECT @WORKDAYS=COUNT(WEEKDAYS) FROM WORKINGDAYS WHERE isweekend is null

     RETURN @WORKDAYS
END

答案 1 :(得分:1)

您收到错误,因为变量@WORKDAYS的分配操作是错误的。像这样改变

ALTER FUNCTION [dbo].[fnGetWorkingDays]
(
    @StartDate DATETIME,
    @EndDate DATETIME
)
RETURNS INT
AS
BEGIN
    DECLARE @dateFrom DATETIME;
    DECLARE @dateTo DATETIME;
    SET @dateFrom = @StartDate;
    SET @dateTo = @EndDate;
    DECLARE @WORKDAYS INT;

    WITH CTE
    AS 
    (
       SELECT 
          @STARTDATE AS STARTDATE
       UNION ALL
       SELECT 
          DATEADD(D, 1, STARTDATE)
          FROM CTE
             WHERE STARTDATE < @ENDDATE
    ),WORKINGDAYS
    AS 
    (
       SELECT
          DATENAME(DW, STARTDATE) WEEKDAYS,
          C1.CalanderDayName AS isweekend
          FROM CTE c
             LEFT JOIN HRM.tbl_Calendar C1 
                ON DATENAME(DW, STARTDATE) = C1.CalanderDayName
                    AND C1.IsOffDay = 1
    )
    SELECT 
       @WORKDAYS = COUNT(WEEKDAYS)--Asign Variable Here
       FROM WORKINGDAYS
          WHERE isweekend IS NULL;

    RETURN @WORKDAYS;

END;

答案 2 :(得分:0)

您编写了错误的语法来设置@WORKDAYS,请尝试以下语法

CREATE FUNCTION [dbo].[fnGetWorkingDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN        

    DECLARE @WORKDAYS INT   

    ;WITH CTE AS (
    SELECT @STARTDATE  AS STARTDATE
    UNION ALL
    select DATEADD(D,1,STARTDATE) 
    FROM CTE
    WHERE STARTDATE <@ENDDATE
    )
    ,WORKINGDAYS AS (
    SELECT STARTDATE,DATENAME(DW,STARTDATE)WEEKDAYS, C1.CalanderDayName AS isweekend
    FROM CTE c
    LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE)=C1.CalanderDayName AND C1.IsOffDay=1
    )

    SELECT @WORKDAYS=COUNT(WEEKDAYS) FROM WORKINGDAYS WHERE isweekend is null

     RETURN @WORKDAYS
END