-1

I am trying to get this command that is on auto run to have an automatic delete incase I have to re-run the application. I am using visual studio and sql server 2012.

Here is what I have. The Create table works but its the IF EXISTS that I am having trouble with.

IF EXISTS (DROP TABLE ST_BANLIST)

CREATE TABLE ST_BANLIST
(BAN VARCHAR (9).
CALL_ACTIVITY CHAR(1).
BAN_STATUS CHAR(1))

Thanks for any help

M.Ali
  • 67,945
  • 13
  • 101
  • 127
  • 2
    Possible duplicate of [How to drop a table if it exists in SQL Server?](http://stackoverflow.com/questions/7887011/how-to-drop-a-table-if-it-exists-in-sql-server) – Daniel Corzo Dec 27 '16 at 21:00

2 Answers2

1

Your syntax was incorrect:

IF OBJECT_ID('dbo.ST_BANLIST', 'U') IS NOT NULL
    DROP TABLE dbo.ST_BANLIST

SQL Server 2016 makes this a lot easier (what took so long Microsoft?):

DROP TABLE IF EXISTS dbo.ST_BANLIST
Code Different
  • 90,614
  • 16
  • 144
  • 163
0
if exists(select * from  sys.objects where name ='MytableName' and type='U')
    Drop table dbo.MytableName
Vigya
  • 122
  • 1
  • 7