Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • SQL : How to check if a stored procedure exists before creation?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.50k
    Comment on it

    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
    

    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
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: