Q:

How to get existence of a temporary table in sql server 2008

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

A:

Try just using the OBJECT_ID function to determine if the temp table exists:
SELECT object_id('tempdb..#nima')

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:
SELECT OBJECT_NAME(OBJECT_ID('tempdb..#nima'), DB_ID('tempdb'))

