How to alter an existing stored procedure in a SQL Server database? Simply use the alter command instead of the create command one to modify an existing SQL procedure. In this example let’s add the grouping on months and employees to sum up and group data. Let’s add also a sorting option to have the Sales figures by descending amounts.
How to alter a SQL Server stored procedure?
This T-SQL command modify the existing uspListSales stored procedure to get the sales for a given customer and to get the figures sorted by amounts from the more important to the less important one.
ALTER PROCEDURE uspListSales AS SELECT MonthName, EmployeeNumber, SUM(NumberOfSales) AS SumNumberOfSales, SUM(AmountIT) AS SumAmountIT FROM dbo.SALES GROUP BY MonthName, EmployeeNumber ORDER BY SUM(AmountIT) DESC GO
The new stored procedure is now grouping and sorting the sales amount
This command will modify the exiting object in order now to display the month name, the employee number, the total number of sales as well as total amount of sales including taxes. The changes made to the stored procedure also sort the lines returned based on the total sales amounts in descending order.