?
Solved

check if table exist if so drop it.

Posted on 2007-03-29
3
Medium Priority
?
398 Views
Last Modified: 2011-10-03
--Drop temp tables
        /* DROP TABLE #Tmp_PrestigeCardList
         DROP TABLE #Tmp_PC_Purchase
         DROP TABLE #TMP_COMBO_STORE_DEF
         DROP TABLE #Tmp_LN_Discount
         DROP TABLE #Tmp_Trans_With_Prestige_Discount
         DROP TABLE #Tmp_Cust_Distinct_Trans
         DROP TABLE #Tmp_Prestige_Sales
         DROP TABLE #Tmp_Prestige_Discount
         DROP TABLE #TMP_DATA */

I have to check the temp table exist if so drop it.
0
Comment
Question by:mathieu_cupryk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
lahousden earned 2000 total points
ID: 18818009
 IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects
             where xtype = 'U'
             and name = '#Tmp_PrestigeCardList')
   begin
    DROP Table #Tmp_PrestigeCardList
   end
and so on...
0
 
LVL 16

Expert Comment

by:rboyd56
ID: 18818040
The problem with checking for the existence of a temp table is that the table name, as stored by the system includes other information other than the name you specify:

If I create a table named #Tmp_PrestigeCardList
this is the name returned by select name from sysobjects

#Tmp_PrestigeCardList_______________________________________________________________________________________________0000000000DD

Another issue is that multple users may be creating tables with the same base name.

There really is no reason to check for the existence. You can just drop it without checking. You may have to capture the error and do something if it occurs but in most cases the error will have no effect on subsequent commnads
0
 
LVL 16

Expert Comment

by:rboyd56
ID: 18818124
You may find that you have to do something like this instead:

 IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects
             where xtype = 'U'
             and name like '%#Tmp_PrestigeCardList%')
   begin
    DROP Table #Tmp_PrestigeCardList
   end
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

771 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