Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Creating Stored Procedures in SQL


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.

What is a Stored Procedure?

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.

Benefits of Stored Procedures

Creating a Stored Procedure in SQL

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:

Basic Example: Creating a Simple 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;
    

Creating Stored Procedures with Parameters

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:

Example: Creating a Stored Procedure with 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.

Returning Output from Stored Procedures

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:

Example: Stored Procedure with Output Parameter

      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.

Modifying and Dropping Stored Procedures

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:

Modifying a Stored Procedure

      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.

Dropping a Stored Procedure

      DROP PROCEDURE GetCustomerByCountry;
    

This command removes the GetCustomerByCountry stored procedure from the database.

Conclusion

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.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java