How to create a temp table and a table if it not exits only.

How do i create temporary table tempt.
I have to create it in procedure, and everytime, procedure is called the table should be created and old table should be delted.
searchsanjaysharmaAsked:
Who is Participating?
 
Pratima PharandeConnect With a Mentor Commented:
use this statement before creating tables , replace #tmptable with your tablename


IF OBJECT_ID('tempdb..#tmptable') IS NOT NULL
BEGIN      
      DROP TABLE #tmptable
END
0
 
waltersnowslinarnoldConnect With a Mentor Commented:
A sample structure is defined below, This should assist you.

CREATE TABLE #tempTableName
(
col1 DATATYPE,
col2 DATATYPE,
..
..
)
0
 
Bhavesh ShahConnect With a Mentor Lead AnalysistCommented:
0
 
jogosConnect With a Mentor Commented:
Comments above are good. But your question let me doubt the term 'temp table'.  

In sql server you have temp tables that after your session is finished will be dropped automaticly (so normaly should not exists at next run, except when you do it in query-window without ending the batch), let table start with # (see post before),  with## you get  a global temp table http://www.codeproject.com/Articles/42553/Quick-Overview-Temporary-Tables-in-SQL-Server-2005

You can test before you want to create a table if it does not already exists (see higher).
0
 
searchsanjaysharmaAuthor Commented:
ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.