IMG-LOGO

How to check if stored procedure exists in SQL Server?

andy - 14 Oct, 2019 9647 Views 0 Comment

There is a case when you need to check if a stored procedure in SQL Server exists. Especially if you build an application that requires some upgrade. There is a time you need to replace the existing stored procedure with the new one.

Below is the SQL query to check if the existing stored procedure and delete the existing stored procedure.

IF EXISTS (SELECT * from dbo.sysobjects WHERE Id = object_id(N'dbo.MyStoredProcedureName') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE dbo.MyStoredProcedureName
GO

Once it has been deleted, you can delete it again. Alternatively, if you prefer not to delete it, you can use the option to update by using the keyword ALTER.

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to restore database using SQL query in TSQL?

If you have a database backup bak file extension and want to restore it using SQL query You can use the built in RESTORE DATABASE function Remember in order to be able to restore a database successfully You need to ...

How to get all table sizes in TSQL?

To get the information about how much space or size used by tables You can retrieve the size information by linking multiple tables in sys tables There are two tables that hold this information The first one is the sys ...