Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Drop table if exists

Author  Topic 

olibara
Yak Posting Veteran

94 Posts

Posted - 2012-02-22 : 07:03:37
Hello

In mysql there is a very simple way to test if a table exists before to drop it

DROP Table IF EXISTS 'MaTable'

I do not found any equivallent in Sql Server
How can I do that on a simple way ?

Thanks for your help

Mahdi Eftekhari Moghaddam
Starting Member

3 Posts

Posted - 2012-02-22 : 07:07:00
[code]IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[YourTableName]') AND type in (N'U'))
DROP TABLE [dbo].[YourTableName][/code]

If it does not help please let me know

Regards
Mahdi Eftekhari
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-02-22 : 07:31:45
if (OBJECT_ID('YourTableName') >0 )
Drop table 'YourTableName'

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

olibara
Yak Posting Veteran

94 Posts

Posted - 2012-02-22 : 12:10:10
Thank You

It is not so easy as in MySql but Ok !
Go to Top of Page
   

- Advertisement -