Solved

check if table exist if so drop it.

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

728 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