Solved

check if table exist if so drop it.

Posted on 2007-03-29
3
371 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Live Storage Vmotion VMs with shared VMDK 10 57
hyperlink data type in SQL 3 27
always on switch back after failover 2 33
Return 0 on SQL count 24 29
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 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