How about this?
-- Using Common Table Expression (CTE) to get Ecode date ranges to process
WITH EcodeDateRanges AS
(
SELECT dayBefore.Ecode, cl.Startdate, cl.Enddate FROM dbo.Absentdata dayBefore
JOIN dbo.ContLeave cl ON dayBefore.Absentdate = DATEADD(DAY,-1,cl.Startdate)
JOIN dbo.Absentdata dayAfter ON cl.Enddate = DATEADD(DAY,-1,dayAfter.Absentdate)
WHERE dayBefore.Ecode = dayAfter.Ecode
),
-- Using a recursive CTE to get the individual dates covered by a range
dateLists AS
(
SELECT edr.Ecode, edr.Startdate AS AbsentDate, edr.Enddate
FROM EcodeDateRanges edr
UNION ALL
SELECT dl.Ecode, DATEADD(DAY,1,dl.AbsentDate), dl.Enddate
FROM dateLists dl
WHERE DATEADD(DAY,1,dl.AbsentDate) <= dl.Enddate
)
--INSERT dbo.C15LL ( Ecode, Absentdate )
SELECT dl.Ecode, dl.AbsentDate FROM dateLists dl;
Forum Etiquette: How to post data/code on a forum to get the best helpMake sure that you include code in the appropriate IFCode tags, e.g. <your code here>
. You can find the IFCode tags under the INSERT options when you are writing a post.