Temp tables are not automatically dropped at the end of a query only when the current database connection is deleted or you explicitly delete them using DROP TABLE #columntable
Either check the table at the beginning of the query, or alwayas delete it at the end (preferably both)
EDIT: As Matrin said in his comment, this is actually a parsing error. You get the same error if you just parse SQL, as when executing it.
To test this, I split your request and tried:
if exists (select 1 from emp where id = 6) create table #columntable (newcolumns varchar(100)) GO if not exists (select 1 from emp where id = 6) create table #columntable (oldcolumns varchar(100)) GO
The parser is pleased with this. Interestingly, if you switch to using non-temp tables, the original query will understand perfectly (I understand the problems that would create, I was just interested to find out why the query would not be analyzed).
Tony
source share