We help IT Professionals succeed at work.

Drop table if exists, MSSQL

Hi Experts,

How can I drop a table in MSSQL if it exists?

I have tried both

DROP TABLE IF EXISTS dbo.agent_rates

and

IF OBJECT_ID (dbo.agent_rates) IS NOT NULL
BEGIN
      DROP TABLE dbo.agent_rates
END

Both do not work

Any help will be greatly appreciated
Comment
Watch Question

Operational DBA
Commented:
I think you're looking for:

if object_id( N'dbo.agent_rates' ) is not null
     drop table dbo.agent_rates;

Open in new window


(yours should error out from the lack of single quotes around your string, it's expecting an nvarchar or varchar be passed - and the N just signifies a unicode string [which is just my personal preference] )
APD TorontoSoftware Developer

Author

Commented:
Thank you!