Google
 

Tuesday, June 17, 2008

TSQL Stored Procedures » Modifying a Stored Procedure

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.

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

No comments: