Stored procedures are a set of SQL statements that are stored in the database and can be executed on demand. They allow for the encapsulation of complex business logic, data manipulation, and repetitive tasks. Using stored procedures in SQL can help improve performance, ensure code reuse, and provide better security for database operations.
A stored procedure is a precompiled collection of one or more SQL statements that can be executed together as a single unit. Stored procedures can accept input parameters, execute queries, perform calculations, and return results. They can also include control-of-flow constructs like IF, WHILE, and BEGIN/END for conditional logic and loops.
To create a stored procedure, we use the CREATE PROCEDURE statement followed by the procedure name and the SQL logic that should be executed. Below is an example of how to create a basic stored procedure:
CREATE PROCEDURE GetCustomerInfo AS BEGIN SELECT CustomerID, CustomerName, ContactName, Country FROM Customers; END;
This stored procedure, named GetCustomerInfo, retrieves all records from the Customers table. Once created, it can be executed using the EXEC or EXECUTE command:
EXEC GetCustomerInfo;
Stored procedures can accept input parameters to make them more dynamic. Parameters allow you to pass values into the procedure at runtime. Here’s an example of a stored procedure with input parameters:
CREATE PROCEDURE GetCustomerByCountry @Country VARCHAR(50) AS BEGIN SELECT CustomerID, CustomerName, ContactName, Country FROM Customers WHERE Country = @Country; END;
In this example, the stored procedure GetCustomerByCountry accepts a @Country parameter. When calling the procedure, we pass the desired country name:
EXEC GetCustomerByCountry 'Germany';
The above query will return all customers from Germany.
Stored procedures can return values or results through output parameters or result sets. Here’s an example of using an output parameter to return a value from a stored procedure:
CREATE PROCEDURE GetTotalOrders @CustomerID INT, @TotalOrders INT OUTPUT AS BEGIN SELECT @TotalOrders = COUNT(OrderID) FROM Orders WHERE CustomerID = @CustomerID; END;
In this stored procedure, @TotalOrders is an output parameter that returns the count of orders for a specified CustomerID. When calling the procedure, we use the OUTPUT keyword to capture the result:
DECLARE @Total INT; EXEC GetTotalOrders 1, @Total OUTPUT; PRINT 'Total Orders: ' + CAST(@Total AS VARCHAR);
This will return the total number of orders for the customer with CustomerID = 1.
You can modify an existing stored procedure using the ALTER PROCEDURE statement. If you no longer need a stored procedure, you can remove it with the DROP PROCEDURE statement:
ALTER PROCEDURE GetCustomerByCountry @Country VARCHAR(50) AS BEGIN SELECT CustomerID, CustomerName, ContactName, Country, City FROM Customers WHERE Country = @Country; END;
This modification adds the City column to the result set.
DROP PROCEDURE GetCustomerByCountry;
This command removes the GetCustomerByCountry stored procedure from the database.
Stored procedures are a powerful feature of SQL that allow developers to encapsulate business logic, improve performance, and ensure better security and maintainability of database operations. By understanding how to create, modify, and manage stored procedures, you can make your database-driven applications more efficient and easier to manage.