Monday, 15 December 2008

Check if database table, view, stored procedure exists

-- check if table exists
if exists (select * from dbo.sysobjects where id = object_id(N'[TableName]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  -- do something
GO
-- check if view exists
if exists (select * from dbo.sysobjects where id = object_id(N'[ViewName]') and OBJECTPROPERTY(id, N'IsView') = 1)
  -- do something
GO
-- check if stored procedure exists
if exists (select * from dbo.sysobjects where id = object_id(N'[StoredProcedureName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  -- do something
GO

No comments:

Post a Comment