SQLScholar
Programmer
Hey all,
I am still dont 100% understand CTEs. We have a little issue with a CTE that doesnt stop running. Basically i need to create a list of every date, cross joined to a table with about 10 rows.
Without the cross join element it works perfectly. With it - it starts off working ok. Then it gets todays date - and then repeats it for infinity.
heres my code:
Any help gratefully recieved.
Dan
----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Dr. Seuss
Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
I am still dont 100% understand CTEs. We have a little issue with a CTE that doesnt stop running. Basically i need to create a list of every date, cross joined to a table with about 10 rows.
Without the cross join element it works perfectly. With it - it starts off working ok. Then it gets todays date - and then repeats it for infinity.
heres my code:
Code:
with ReportDates as
(
select cast('2011-01-07' as date) calendar_date, ORGANISATION_CODE
from
dbo.ORGANISATION_UNITS
where LEN(ORGANISATION_UNITS.ORGANISATION_CODE) = 1
union all
select DATEADD(DAY,1,calendar_date), ORGANISATION_UNITS.ORGANISATION_CODE
from ReportDates
cross join
dbo.ORGANISATION_UNITS
where DATEADD(DAY,1,calendar_date) < GETDATE()
and LEN(ORGANISATION_UNITS.ORGANISATION_CODE) = 1
)
select * from reportdates
OPTION (MAXRECURSION 0)
Any help gratefully recieved.
Dan
----------------------------------------
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Dr. Seuss
Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------