在INSERT查询中使用maxrecursion

时间:2014-01-21 13:12:30

标签: sql-server tsql insert common-table-expression recursive-query

感谢您关注此事。

我有两个生成工作时间的CTE表,计算结果很复杂,所以不会打扰你。

这是SQL(简体)

DECLARE @StartDate Datetime -- Start Date for the Duties to be generated
DECLARE @PatternStartPoint int --Start Point in Pattern eg 3 - 
DECLARE @PatternSize int --The length of the shift pattern
DECLARE @User int 
SET @User = 1111;
SET @PatternSize = 3;
SET @DaysRequired=16; --Testing assume 16
SET @StartDate = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())); --for testing use today
SET @PatternStartPoint =3;


--Get Job Duty Pattern
WITH Pattern (P_DayNumber, P_DayType, P_Start, P_End)
AS
(
Select              D_DayNumber,D_Type,D_Start,D_End
from                dbo.Tbl_Jobs as J
join                dbo.Tbl_Patterns as P
on                  J.J_PatternID = P.Pattern_ID
join                dbo.Tbl_PatternDetails as D
on                  P.Pattern_ID=d.D_Pattern_ID
)
,
--Get DateList
Datelist (MyDate, DayNumber) AS 
(
    SELECT @StartDate AS MyDate, @PatternStartPoint as DayNumber
    UNION ALL
    Select MyDate + 1,  (DayNumber+1) - (CAST((Daynumber+1)/(@PatternSize+.01) as int)*@PatternSize) --% (@PatternSize)
    FROM Datelist
    WHERE MyDate < (@Startdate + @DaysRequired)-1

) 

INSERT INTO [IDAHO].[dbo].[Appointments]
           ([Subject]
           ,[Description]
           ,[Start]
           ,[End]
           ,[RoomID]
           ,[UserID]
           )
            (    
            SELECT  
                'Standard Work Pattern'     
                ,'IDAHO Generated on ' + CONVERT(nvarchar(20),getdate())
                ,mydate + p.P_Start
                ,mydate + p.P_End
                ,1
                ,@User as A_User
            FROM            Datelist as d
            join            Pattern as p
            on          p.P_DayNumber=d.DayNumber
            Where           mydate + p.P_Start is not null
            OPTION (MAXRECURSION 0)

            )

我收到MAXRECURSION行的错误,如果我删除该行并尝试生成超过100个“Duties”错误(作为默认值)。

如果我将最后一个语句更改为一个简单的选择它似乎工作???如下图所示

DECLARE @StartDate Datetime -- Start Date for the Duties to be generated
DECLARE @PatternStartPoint int --Start Point in Pattern eg 3 -
DECLARE @PatternSize int --The length of the shift pattern
DECLARE @DaysRequired int --The length of the shift pattern
DECLARE @User int
SET @User=1111;
SET @PatternSize = 3;
SET @DaysRequired=160; --Testing assume 16
SET @StartDate = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())); --for testing use today
SET @PatternStartPoint =3;


--Get Job Duty Pattern
WITH Pattern (P_DayNumber, P_DayType, P_Start, P_End)
AS
(
Select              D_DayNumber,D_Type,D_Start,D_End
from                dbo.Tbl_Jobs as J
join                dbo.Tbl_Patterns as P
on                  J.J_PatternID = P.Pattern_ID
join                dbo.Tbl_PatternDetails as D
on                  P.Pattern_ID=d.D_Pattern_ID
)
,
--Get DateList
Datelist (MyDate, DayNumber) AS 
(
    SELECT @StartDate AS MyDate, @PatternStartPoint as DayNumber
    UNION ALL
    Select MyDate + 1,  (DayNumber+1) - (CAST((Daynumber+1)/(@PatternSize+.01) as int)*@PatternSize) --% (@PatternSize)
    FROM Datelist
    WHERE MyDate < (@Startdate + @DaysRequired)-1

) 

            SELECT  
                            'Standard Work Pattern'     
                            ,'IDAHO Generated on ' + CONVERT(nvarchar(20),getdate())
                            ,mydate + p.P_Start
                            ,mydate + p.P_End
                            ,1
                            ,@User as A_User
            FROM            Datelist as d
            join            Pattern as p
            on              p.P_DayNumber=d.DayNumber
            Where           mydate + p.P_Start is not null
            OPTION (MAXRECURSION 0)

我把命令放在了错误的地方吗? 为什么它适用于select而不是insert?

谢谢

1 个答案:

答案 0 :(得分:0)

我认为这是因为在最终选择运行之前CTE最终没有被组装到结果集中,这就是巡回第二个查询有效的原因。要使插入工作,请尝试:

DECLARE @StartDate Datetime -- Start Date for the Duties to be generated
DECLARE @PatternStartPoint int --Start Point in Pattern eg 3 -
DECLARE @PatternSize int --The length of the shift pattern
DECLARE @DaysRequired int --The length of the shift pattern
DECLARE @User int
SET @User=1111;
SET @PatternSize = 3;
SET @DaysRequired=160; --Testing assume 16
SET @StartDate = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())); --for testing use today
SET @PatternStartPoint =3;


--Get Job Duty Pattern
INSERT INTO [IDAHO].[dbo].[Appointments]
           ([Subject]
           ,[Description]
           ,[Start]
           ,[End]
           ,[RoomID]
           ,[UserID]
           )
select *
from
(
    SELECT  
        'Standard Work Pattern'     [Subject]
        ,'IDAHO Generated on ' + CONVERT(nvarchar(20),getdate()) [Description]
        ,mydate + p.P_Start [Start]
        ,mydate + p.P_End [End]
        ,1 [RoomID]
        ,@User [UserID]
    FROM            
    (
        SELECT @StartDate AS MyDate, @PatternStartPoint as DayNumber
        UNION ALL
        Select MyDate + 1,  (DayNumber+1) - (CAST((Daynumber+1)/(@PatternSize+.01) as int)*@PatternSize) --% (@PatternSize)
        FROM Datelist
        WHERE MyDate < (@Startdate + @DaysRequired)-1
    ) as d
    join
    (
        Select D_DayNumber,D_Type,D_Start,D_End
        from                dbo.Tbl_Jobs as J
        join                dbo.Tbl_Patterns as P
        on                  J.J_PatternID = P.Pattern_ID
        join                dbo.Tbl_PatternDetails as D
        on                  P.Pattern_ID=d.D_Pattern_ID
    ) as p
    on              p.P_DayNumber=d.DayNumber
    Where           mydate + p.P_Start is not null
) x;

这可能有效。

(根据要求扩大) (修改后删除CTE)