T-SQL » Stored Procedures » Modifying a Stored Procedure

Syntax:
ALTER PROC [ EDURE ] procedure_name [ ;number ]
    [ { @parameter data_type }
       [ VARYING ] [ =default ] [ OUTPUT ]
    ] [ ,... ]
[ WITH
    {
       RECOMPILE
       | ENCRYPTION
       | RECOMPILE, ENCRYPTION
    }
]
[ FOR REPLICATION ]
AS
    sql_statement

The stored procedure modification is usually done using the ALTER PROCEDURE statement. It can also be done by deleting the existing stored procedure and then creating a new one with the same name. If we are using the ALTER PROCEDURE statement any of the permissions associated with the stored procedure are retained. In the other case, however, the permissions will be lost.

We can alter a stored procedure so that only the parameter definition is changed and not the permissions that are set for the stored procedure. The parameters from the ALTER PROCEDURE statement are the same as the ones for the CREATE PROCEDURE statement.

Examples

Code:
CREATE PROCEDURE spGetAvgGrade
AS
SELECT AVG(Std_Grade) FROM Students
GO
EXEC spGetAvgGrade;
Output:
AverageGrade
2

(1 row(s) affected)
Language(s): MS SQL Server
Code:
ALTER PROCEDURE spGetAvgGrade (@Course INTEGER)
AS
SELECT AVG(Std_Grade) as AverageGrade FROM Students
WHERE Std_Course = @Course
GO

EXEC spGetAvgGrade 3;
Output:
AverageGrade
1

(1 row(s) affected)
Explanation:

This example demonstrates how to use the ALTER PROCEDURE command to modify a procedure and then execute it.

Language(s): MS SQL Server
Code:
DROP PROCEDURE spGetAvgGrade;

CREATE PROCEDURE spGetAvgGrade (@Course INTEGER)
AS
SELECT AVG(Std_Grade) as AverageGrade FROM Students
WHERE Std_Course = @Course
GO

EXEC spGetAvgGrade 3;
Output:
The command(s) completed successfully.


AverageGrade
1

(1 row(s) affected)
Explanation:

In the above example, we first delete the existing procedure and then recreate the procedure.

Language(s): MS SQL Server