Solved

check if table exist if so drop it.

Posted on 2007-03-29
3
352 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
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
lahousden earned 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

22 Experts available now in Live!

Get 1:1 Help Now