A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
So I don't know what is going on, but I can see that this is a case odd enough to slip through the cracks. Also, I don't have access on a repro, and you don't seem to keen on sharing that much detail in a public forum. That is understandable, but that also makes it more difficult to assist.
Here are three ideas:
- Use a table variable instead of a temp table.
- Add the hint OPTION (KEEPFIXED PLAN)
- Open a support case. Assuming that this is a bug in the product, that case you be free of charge of you in the end.
The two first points are shots in the dark based on the idea that the issue is related to recompilation.
By the way, do you have Query Store enabled for the database?