Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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.
0
searchsanjaysharma
Asked:
searchsanjaysharma
4 Solutions
 
waltersnowslinarnoldCommented:
A sample structure is defined below, This should assist you.

CREATE TABLE #tempTableName
(
col1 DATATYPE,
col2 DATATYPE,
..
..
)
0
 
Pratima PharandeCommented:
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
 
Bhavesh ShahLead AnalysistCommented:
0
 
jogosCommented:
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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now