1

What is the best way to drop a table? The code I have does it two different ways. It will be dropped every week when I do a bulk insert. Thanks.

Like this?

IF EXISTS (SELECT * 
           FROM DBO.SYSOBJECTS
           WHERE ID = OBJECT_ID(N'[DBO].[ZIP]') AND OBJECTPROPERTY(ID, N'IsUserTable') = 1) 
    DROP TABLE [DBO].[ZIP]

Or this?

DROP TABLE DEID.DBO.ZIP
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sm86
  • 79
  • 7

1 Answers1

1

The second method will throw an error if the table doesn't exist, or if other database objects depend on it. The first one will not throw an error if the table doesn't exist, but it will still throw an error if other database objects depend on it.

Check out this answers on Stack-overflow:

Ale Gu
  • 146
  • 9
  • Thanks. Should I just use DROP TABLE IF EXISTS DEID.DBO.ZIP instead of the where statement? – sm86 Jul 08 '20 at 18:20