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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TSQL mapping detailed records to group records 9 51
Please help for the below sql query. 1 24
Help Required 2 32
Present Absent from working date rage 11 20
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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