Thank you for looking at this.
I have two CTE tables that generate duty times, the calculations are convoluted so wont bother you with them.
Here is the SQL (Simplified)
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)
)
I am getting an error with the MAXRECURSION line, if I remove the line and try to generate more than 100 'Duties' it errors (being the default value).
If I Change the last statement to a simple select it seems to work??? as shown below
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)
Have I put the command in the wrong place? Why does it work with select but not insert?
Thank you
↧