Tuesday, March 20, 2012

Conditionally dropping a table

To All

How do I check to see if a table exists before I drop it.

Thanks

David Davisif exists (select * from dbo.sysobjects where id = object_id(N'[MyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [MyTable]
GO
|||Tyler Free

Thanks works like a charm

David Davis|||The easiest way to check this in SQL Server 2005 is to use OBJECT_ID. It now exposes an additional type parameter which is useful for checking schema scoped objects. This parameter was not documented in SQL2000. So you can just do:

if object_id('[MyTable]', 'U') is not null
drop table [MyTable];sqlsql

No comments:

Post a Comment