Blog

Checking a Stored Procedure exists or not in database

Posted by Hemant Srivastava Blogs | SQL Nov 29, 2012
A SQL script that checks a strored procedure exists or not . If it is, it deletes the stored proc before running its creation script.

When we keep all our stored procedures creation script in a file and want to run that whole script. It always complains about already existing stored procedure having same name.

 

It is better before running your Stored Proc creation script, your creation script should be smart enough that always checks the presence of same name stored procedure before.

 

Following piece of SQL Script checks the stored procedure presence. If it finds, it delets the old one and create a new one having same name (of course most of the time, the new one would be modified)

When we keep all our stored procedures creation script in a file and want to run that whole script. It always complains about already existing stored procedure having same name.

 

It is better before running your Stored Proc creation script, your creation script should be smart enough that always checks the presence of same name stored procedure before.

 

Following piece of SQL Script checks the stored procedure presence. If it finds, it delets the old one and create a new one having same name (of course most of the time, the new one would be modified)


 

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

drop procedure [dbo].[ StoredProcedureName]

GO

ChkStoredProcPresence.jpg

comments
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts