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

Posted on 2012-04-01
Last Modified: 2012-04-02
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.
Question by:searchsanjaysharma

Assisted Solution

waltersnowslinarnold earned 125 total points
ID: 37794756
A sample structure is defined below, This should assist you.

CREATE TABLE #tempTableName
LVL 39

Accepted Solution

Pratima Pharande earned 125 total points
ID: 37794758
use this statement before creating tables , replace #tmptable with your tablename

IF OBJECT_ID('tempdb..#tmptable') IS NOT NULL
      DROP TABLE #tmptable
LVL 19

Assisted Solution

by:Bhavesh Shah
Bhavesh Shah earned 125 total points
ID: 37794810
LVL 25

Assisted Solution

jogos earned 125 total points
ID: 37794854
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

You can test before you want to create a table if it does not already exists (see higher).

Author Closing Comment

ID: 37794883

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Return rows that match the first row 3 39
SQL 2000:  Shrink LDF file 11 34
How to enforce inte 8 32
Truncate vs Delete 63 90
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now