• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

check if table exist if so drop it.

--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
mathieu_cupryk
Asked:
mathieu_cupryk
  • 2
1 Solution
 
lahousdenCommented:
 IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects
             where xtype = 'U'
             and name = '#Tmp_PrestigeCardList')
   begin
    DROP Table #Tmp_PrestigeCardList
   end
and so on...
0
 
rboyd56Commented:
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
 
rboyd56Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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