I am trying to use a recursive CTE to combine multiple calls to a table valued function.
I have a fiddle at: https://dbfiddle.uk/kktqHivz
I have a table called sales
:
create table sales (
date date,
total decimal(6,2)
);
The function ds(date)
selects all the sales for a date:
create function ds(@date date) returns table as
return select date, total from sales where date=@date;
I thought I could put them into a recursive CTE as follows:
with cte as (
select cast('2023-01-01' as date) as n, *
from ds('2023-01-01')
union all
select dateadd(day,1,n),d.*
from cte, ds(n) as d -- can’t use ds(n) or ds(cte.n)
where n<'2023-01-05'
)
select * from cte order by n option(maxrecursion 100);
but the message I get is:
Invalid column name 'n'
I’m stuck at this point. The n
is a column from the CTE, and normally you can us it in the recursive member, as you see with the SELECT
clause. Somehow I can’t use it as an argument for the ds()
TVF.
Is there a way I can use the generated date n
in the table valued function?