Skip to content
Advertisement

SQL SERVER: OPTION(MAXRECURSION n) not working with dynamic variables

Techies, This more or less works when the date range is between 1/1/-1/3. I have 2 problems I need to solve. The first is that SQL Server will not let me do this: OPTION (MAXRECURSION @recdays). The next, I am pulling results for 1/4, when I only want results up to 1/3. Any advice for getting this to work? I tried turning the query into something I could run through EXEC sp_executesql. Then I had a new problem–I couldn’t write out to #StoreXJoinDate since that spawns another session. If I convert to ##StoreXJoinDate I can’t kill it from the outer session because that session doesn’t own it.

Here are the insert statements to build Dimension.Stores

Advertisement

Answer

Just use that recursive CTE to load Date table. Like this:

Then to cross-join a date range with another table, run a query like:

or the equivalent

If each store has a different window, then use APPLY eg

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement