You can modify the text of a stored procedure by using the ALTER PROCEDURE statement. The syntax for ALTER PROCEDURE is similar to the syntax for CREATE PROCEDURE (see Listing 1). Using ALTER PROCEDURE
has a couple advantages over dropping and re-creating a procedure to
modify it. The main advantage is that you don’t have to drop the
procedure first to make the change, so it remains available, even if the
ALTER PROCEDURE command fails due to a syntax or object reference error. The second advantage is that because you don’t have to drop the procedure, you don’t have to worry about reassigning permissions to it after modifying it.
Listing 1. Modifying a Stored Procedure by Using ALTER PROCEDURE
ALTER PROCEDURE title_authors @state char(2) = '%'
AS
BEGIN
SELECT a.au_lname, a.au_fname, t.title, t.pubdate
FROM titles t
INNER JOIN titleauthor ta ON t.title_id = ta.title_id
RIGHT OUTER JOIN authors a ON ta.au_id = a.au_id
where state like @state
RETURN
END
|
Viewing and Modifying Stored Procedures with SSMS
You can also use SSMS to create, view, and modify stored procedures.
To edit a stored procedure in SSMS, expand the Programmability folder and then the Stored Procedures folder, right-click the name of the procedure you want to modify, and select Modify (see Figure 1).
SSMS then extracts the ALTER PROCEDURE
statement for the selected procedure into a new query window. Here, you
can edit the procedure code as needed and then execute the contents of
the query window to modify the procedure. In addition, the Object
Browser in SSMS provides other options for extracting the stored
procedure source code. It can generate code to create, alter, or drop
the selected stored procedure. You can script the stored procedure
source code to a new window, to a file, or to the Windows Clipboard by
right-clicking the stored procedure name in the Object Browser and
choosing the appropriate option (see Figure 2).