posts - 50, comments - 92, trackbacks - 171

My Links

News

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' 

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
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: