views:

534

answers:

1

I have a CTE that's doing some recursion. The easiest way to imagine the problem space is a resistor that is used in cars all over the world and you have all that information. Resistor A is used in Board B and C which is used in DashAssembly D,E,F...ZZ which is used in Car AAA, AAB and AAC.

I got the CTE working with one part, checked the results, everything's fine (using a MAXRECURSION of 100). Then I add in a part that's a little more common in the part trees. Boom, broken. I tried bumping up the MAXRECURSION to 32767 and it still broke. Looking at the data, this particular part is used all over the place. At the second level of recursion it has 426 parents. And so on, 6 levels of recursion I think. It makes sense (I think) that this is bombing out MAXRECURSION.

Next question is, what's plan B? I've never done any recursion that didn't use CTEs.

Update: the answer is: make sure you don't have any circular references and make sure that your recursion table is filtering its join properly (I didn't do the second one properly). Basically I goofed up and thought that my it was my crazy wild data that was the issue when instead it was my crappy sql skills.

+1  A: 

Most often, if you're hitting a recursion limit, you bump the limit to $BIGNUM and you're still hitting it, that means you have a circular reference.

You should check to make sure that you don't have any circular references and that your recursion table is filtering properly.

chaos
Msg 310, Level 15, State 1, Line 45The value 32768 specified for the MAXRECURSION option exceeds the allowed maximum of 32767.
jcollum
in other words, you can't go over 32767
jcollum
Yeah. I'm saying you don't need a higher recursion limit, you need data that doesn't contain circular dependencies. Finding your circular dependencies is another problem...
chaos
hmm, tough to find, you might have a good point tho
jcollum
i.e. recursing down circular references will always hit any recursion limit, and really that's what it's there for.
chaos
Argh, you were right, poorly defined references. Add in "check to make sure that you don't have any circular references and that your recursion table is filtering properly" and I'll call this one the answer.
jcollum
It is possible to exceed the 32767 limit. Just set the option to 0. Obviously, this isn't what you wanted to do here. But it works. It basically tells SQL Server to continue forever.
Registered User