over 9 years ago
In this article we will learn how to check if a stored procedure exists before creating it. We can use the below script which will drop the proc if it exists and then recreate it.
- IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_ProcToBeCreated')
- DROP PROCEDURE sp_ProcToBeCreated
- GO
- CREATE PROCEDURE sp_ProcToBeCreated
- AS
- BEGIN
- -----Proc Contents
- END
- GO
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_ProcToBeCreated') DROP PROCEDURE sp_ProcToBeCreated GO CREATE PROCEDURE sp_ProcToBeCreated AS BEGIN -----Proc Contents END GO
Or we can also alter the proc in case it exists as below:
- IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_ProcToBeCreated')
- ALTER PROCEDURE sp_ProcToBeCreated
- AS
- BEGIN
- -----Proc Contents
- END
- GO
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_ProcToBeCreated') ALTER PROCEDURE sp_ProcToBeCreated AS BEGIN -----Proc Contents END GO
0 Comment(s)