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

Comments

Popular posts from this blog

Parse XML to dynamic object in C#

C# Updating GUI from different thread