posts - 50, comments - 154, trackbacks - 169

My Links

News

Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

Friends Blog

Queries to check SP,table,trigger exists in database or not

Simple quries to check whether they are existing in database or not.

1.       Checking whether procedure exist in the NORTHWIND database with name 'CustOrdersDetail' 

Approach -1:

use NORTHWIND

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CustOrdersDetail]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

print 'CustOrdersDetail Exists in the NORTHWIND database'

else

print 'CustOrdersDetail  *does not exist* in the NORTHWIND database' 

Approach -2:

 Use NORTHWIND

if exists(select * from dbo.sysobjects where type = 'p' and name = 'CustOrdersDetail’  )

print ''CustOrdersDetail   Exists in the NORTHWIND database' 

else 

print ''CustOrdersDetail   *DOES not exist* in the NORTHWIND database'

 

2.       Checking whether table exist in the database with name 'EmployeeTerritories'

Approach -1:

 use NORTHWIND

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EmployeeTerritories]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 

print 'EmployeeTerritories Exists in the NORTHWIND database' 

else 

print 'EmployeeTerritories *DOES not exist* in the NORTHWIND database'

 Approach -2: 

use NORTHWIND 

if exists(select * from dbo.sysobjects where type = 'U' and name = 'EmployeeTerritories'  )

 print 'EmployeeTerritories Exists in the NORTHWIND database'

 else

 print 'EmployeeTerritories *DOES not exist* in the NORTHWIND database'

 3.       Checking whether trigger exist in the database with name 'TempTrigger'

 Approach -1:

use Northwind

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TempTrigger]') and OBJECTPROPERTY(id, N'IsTrigger') = 1) 

print 'TempTrigger Exists in the NORTHWIND database'

 else

 print 'TempTrigger *DOES not exist* in the NORTHWIND database'

 Approach -2:

 use Northwind

if exists (select * from dbo.sysobjects where name ='TempTrigger' and type ='TR')

 print 'TempTrigger Exists in the NORTHWIND database'

 else

 print 'TempTrigger *DOES not exist* in the NORTHWIND database' 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Thursday, October 30, 2003 9:32 AM | Filed Under [ SQLServer ]

Feedback

Gravatar

# re: Queries to check SP,table,trigger exists in database or not

Excellent guide. Thanks a lot!
The only thing I want to know, how do you check if the table exists in another database without switching between datatbases.
5/1/2005 6:06 AM | Artem
Gravatar

# re: Queries to check SP,table,trigger exists in database or not

All --

Please help.

I need to how to check for SP existence with only db_datawriter permissions.

I am stuck because the Sql Server 2005 Express login that I use to check for SP existence is only a member of the following UserMappings...

public, db_datawriter

...and, as such, that login does not have permissions to query sysobjects, or so it seems because such a query always returns an empty result...

...so, is there a workaround?

Please advise.

Thank you.

-- Mark Kamoski
12/17/2009 8:06 AM | Mark Kamoski
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: