I wrote this query:
SELECT * INTO #nima FROM Region r
Every time I execute this queries:
SELECT OBJECT_NAME(OBJECT_ID('tempdb..#nima'))
--or
SELECT OBJECT_NAME(OBJECT_ID('#nima'))
I get NULL, but when I execute above select I get error that #nima alreadty exist
Try just using the OBJECT_ID function to determine if the temp table exists:
Or if you wish to retrieve the object name, you will need to specify the database id using the DB_ID function for the temp database: