I think you are going out of scope when you are using tmp table..