Many time we need to check if object exists or not in SQL server when we create new objcet(i.e. DB,Table,View,SP,Functions etc).
There are certain situation when if particular object exist then drop/modify else Create.
Because we need to run script again and again on same DB to avoid any existance error we use check if exists object.
For such cases here i am writing Check if exist query for DB objects.....
----Check If Database Exists----
IF db_id('<DB_Name>') IS NOT NULL
Print 'Database Exists'
ELSE
Print 'Database Does Not Exists'
----Check If Table Exists----
IF OBJECT_ID ('<Table_Name>','U') IS NOT NULL
Print 'Table Exists'
ELSE
Print 'Table Does Not Exists'
----Check If SP Exists----
IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id (N'<SP_Name>') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
Print 'SP Exists'
ELSE
Print 'SP Does Not Exists'
----Check If View Exists----
IF EXISTS (Select * From dbo.sysobjects Where id =
Object_ID('<View_name>') and OBJECTPROPERTY(id, N'IsView') = 1)
PRINT 'View Exists'
ELSE
PRINT 'View Does Not Exists'
----Check If Function Exists----
IF OBJECT_ID(N'<Function_Name>', N'FN') IS NOT NULL
Print 'Function Exists'
Else
Print 'Function Does Not Exists'
----Check If Foreign Key Exists----
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'<ForiegnKey_name>')
AND parent_object_id = OBJECT_ID(N'<Table_name>'))
Print 'Foreign Key Exists'
Else
Print 'Foreign Key Does Not Exists'
----Check If Primary Key Exists----
IF OBJECTPROPERTY( OBJECT_ID( '<Table_name>' ), 'TableHasPrimaryKey' ) = 1
PRINT 'Table has a primary key.'
ELSE
PRINT 'Table has no primary key.'
----Check If Trigger Exists----
IF EXISTS (Select * From dbo.sysobjects Where name ='<Trigger_Name>' And type ='TR')
Print 'Trigger Exists'
Else
Print 'Trigger Does Not Exists'
----Check If Index Exists----
IF EXISTS (Select * From sysindexes Where id=object_id('<Table_name>') And name='<Index_name>')
Print 'Index Exists'
Else
Print 'Index Does Not Exists'
----Check If Column Exists in a table----
END
ELSE
BEGIN
Print 'Column Not Exists'
END
Cheers Guys!!!!!!!!!!
Sandyyy.. :)
There are certain situation when if particular object exist then drop/modify else Create.
Because we need to run script again and again on same DB to avoid any existance error we use check if exists object.
For such cases here i am writing Check if exist query for DB objects.....
----Check If Database Exists----
IF db_id('<DB_Name>') IS NOT NULL
Print 'Database Exists'
ELSE
Print 'Database Does Not Exists'
----Check If Table Exists----
IF OBJECT_ID ('<Table_Name>','U') IS NOT NULL
Print 'Table Exists'
ELSE
Print 'Table Does Not Exists'
----Check If SP Exists----
IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id (N'<SP_Name>') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
Print 'SP Exists'
ELSE
Print 'SP Does Not Exists'
----Check If View Exists----
IF EXISTS (Select * From dbo.sysobjects Where id =
Object_ID('<View_name>') and OBJECTPROPERTY(id, N'IsView') = 1)
PRINT 'View Exists'
ELSE
PRINT 'View Does Not Exists'
----Check If Function Exists----
IF OBJECT_ID(N'<Function_Name>', N'FN') IS NOT NULL
Print 'Function Exists'
Else
Print 'Function Does Not Exists'
----Check If Foreign Key Exists----
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'<ForiegnKey_name>')
AND parent_object_id = OBJECT_ID(N'<Table_name>'))
Print 'Foreign Key Exists'
Else
Print 'Foreign Key Does Not Exists'
----Check If Primary Key Exists----
IF OBJECTPROPERTY( OBJECT_ID( '<Table_name>' ), 'TableHasPrimaryKey' ) = 1
PRINT 'Table has a primary key.'
ELSE
PRINT 'Table has no primary key.'
----Check If Trigger Exists----
IF EXISTS (Select * From dbo.sysobjects Where name ='<Trigger_Name>' And type ='TR')
Print 'Trigger Exists'
Else
Print 'Trigger Does Not Exists'
----Check If Index Exists----
IF EXISTS (Select * From sysindexes Where id=object_id('<Table_name>') And name='<Index_name>')
Print 'Index Exists'
Else
Print 'Index Does Not Exists'
----Check If Column Exists in a table----
IF EXISTS(SELECT * FROM sys.columns Where Name = N'Column_Name' AND Object_ID = Object_ID(N'table_name'))
BEGIN
Print 'Column Already Exists'BEGIN
END
ELSE
BEGIN
Print 'Column Not Exists'
END
Cheers Guys!!!!!!!!!!
Sandyyy.. :)
Comments
Post a Comment