title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | helpviewer_keywords | monikerRange | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Modify a stored procedure |
Learn how to modify a stored procedure in SQL Server by using SQL Server Management Studio or Transact-SQL. |
WilliamDAssafMSFT |
wiassaf |
07/19/2024 |
sql |
stored-procedures |
how-to |
|
|
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric |
[!INCLUDE SQL Server Azure SQL Database Synapse Analytics PDW FabricSQLDB]
This article describes how to modify a stored procedure in [!INCLUDE ssnoversion] by using [!INCLUDE ssManStudioFull] or [!INCLUDE tsql].
[!INCLUDE tsql] stored procedures cannot be modified to be CLR stored procedures and vice versa.
If the previous procedure definition was created using WITH ENCRYPTION
or WITH RECOMPILE
, these options are enabled only if they are included in the ALTER PROCEDURE
statement.
Requires ALTER PROCEDURE permission on the procedure.
To modify a procedure in SQL Server Management Studio:
-
In Object Explorer, connect to an instance of [!INCLUDE ssDE] and then expand that instance.
-
Expand Databases, expand the database in which the procedure belongs, and then expand Programmability.
-
Expand Stored Procedures, right-click the procedure to modify, and then select Modify.
-
Modify the text of the stored procedure.
-
To test the syntax, on the Query menu, select Parse.
-
To save the modifications to the procedure definition, on the Query menu, select Execute.
-
To save the updated procedure definition as a [!INCLUDE tsql] script, on the File menu, select Save As. Accept the file name or replace it with a new name, and then select Save.
Important
Validate all user input. Do not concatenate user input before you validate it. Never execute a command constructed from unvalidated user input. Unvalidated user input makes your database vulnerable a type of exploit called a SQL injection attack. For more information, see SQL injection.
To modify a procedure using T-SQL commands:
-
In Object Explorer, connect to an instance of [!INCLUDE ssDE] and then expand that instance.
-
Expand Databases, expand the database in which the procedure belongs. Or, from the tool bar, select the database from the list of available databases. For this example, select the [!INCLUDE ssSampleDBobject] database.
-
On the File menu, select New Query.
-
Copy and paste the following example into the query editor. The example creates the
Purchasing.uspVendorAllInfo
procedure, which returns the names of all the vendors in the [!INCLUDE ssSampleDBCoFull] database, the products they supply, their credit ratings, and their availability.CREATE PROCEDURE Purchasing.uspVendorAllInfo WITH EXECUTE AS CALLER AS SET NOCOUNT ON; SELECT v.Name AS Vendor, p.Name AS 'Product name', v.CreditRating AS 'Rating', v.ActiveFlag AS Availability FROM Purchasing.Vendor v INNER JOIN Purchasing.ProductVendor pv ON v.BusinessEntityID = pv.BusinessEntityID INNER JOIN Production.Product p ON pv.ProductID = p.ProductID ORDER BY v.Name ASC; GO
-
On the File menu, select New Query.
-
Copy and paste the following example into the query editor. The example modifies the
uspVendorAllInfo
procedure. TheEXECUTE AS CALLER
clause is removed and the body of the procedure is modified to return only those vendors that supply the specified product. TheLEFT
andCASE
functions customize the appearance of the result set.[!IMPORTANT]
Dropping and recreating an existing stored procedure removes permissions that have been explicitly granted to the stored procedure. UseALTER
to modify the existing stored procedure instead.ALTER PROCEDURE Purchasing.uspVendorAllInfo @Product varchar(25) AS SET NOCOUNT ON; SELECT LEFT(v.Name, 25) AS Vendor, LEFT(p.Name, 25) AS 'Product name', 'Rating' = CASE v.CreditRating WHEN 1 THEN 'Superior' WHEN 2 THEN 'Excellent' WHEN 3 THEN 'Above average' WHEN 4 THEN 'Average' WHEN 5 THEN 'Below average' ELSE 'No rating' END , Availability = CASE v.ActiveFlag WHEN 1 THEN 'Yes' ELSE 'No' END FROM Purchasing.Vendor AS v INNER JOIN Purchasing.ProductVendor AS pv ON v.BusinessEntityID = pv.BusinessEntityID INNER JOIN Production.Product AS p ON pv.ProductID = p.ProductID WHERE p.Name LIKE @Product ORDER BY v.Name ASC; GO
-
To save the modifications to the procedure definition, on the Query menu, select Execute.
-
To save the updated procedure definition as a [!INCLUDE tsql] script, on the File menu, select Save As. Accept the file name or replace it with a new name, and then select Save.
-
To run the modified stored procedure, execute the following example.
EXEC Purchasing.uspVendorAllInfo N'LL Crankarm'; GO