Ok, that sound reasonable. I haven't done anything with temporary tables before and didn't realize that a table that was temporary to me was not a temporary table.

Regardless of that, the situation still stands because the behavior is clearly quite strange and entirely reproducible. The way I have it should work, and would work if each line was being executed faithfully each time the query is run. The curiosity is that each line is not being executed in all situations.