Day 11: Unveiling the Power of Stored Procedures and Functions in SQL: Enhancing Code Reusability and Efficiency

Ashimabha Bose
3 min readOct 24, 2023

Follow for content on Investing : LinkedIn / Instagram

Follow for content on Technical: LinkedIn

Navigating the World of Stored Procedures and Functions

In the ever-expanding landscape of SQL, stored procedures and functions stand as pillars of code reusability and efficiency. These versatile tools encapsulate complex logic, simplifying database operations and promoting modularity.

Stored Procedures: Reusable Code Blocks

Stored procedures are precompiled blocks of SQL code stored within the database server. They act as reusable scripts, encapsulating frequently executed tasks and enhancing code organization.

Significance of Stored Procedures:

  • Improved Performance: Stored procedures are precompiled, reducing the overhead of repeatedly parsing and executing SQL statements.
  • Code Reusability: Stored procedures can be called multiple times, eliminating the need to rewrite code for common tasks.
  • Security Enhancement: Stored procedures can restrict access to sensitive data, ensuring controlled data manipulation.

Creating Stored Procedures:

Stored procedures are created using the CREATE PROCEDURE statement, specifying the procedure name, parameters, and the SQL code to be executed. For instance, consider a procedure named 'calculate_order_total' that calculates the total cost of an order:

CREATE PROCEDURE calculate_order_total (
IN order_id INT
)
BEGIN
DECLARE total_cost DECIMAL(10,2);

SELECT SUM(product_price * quantity)
INTO total_cost
FROM order_items
WHERE order_id = order_id;
SELECT total_cost;
END;

Input and Output Parameters:

Stored procedures can accept input parameters to receive data and output parameters to return results. Input parameters are declared using the IN keyword, while output parameters use the OUT keyword.

CREATE PROCEDURE calculate_discounted_price (
IN product_price DECIMAL(10,2),
IN discount_rate DECIMAL(3,2),
OUT discounted_price DECIMAL(10,2)
)
BEGIN
SET discounted_price = product_price * (1 - discount_rate);
END;

User-Defined Functions: Customized Data Transformations

User-defined functions (UDFs) are custom-built functions that extend SQL’s capabilities. They encapsulate complex calculations or data transformations, returning a single value or a table.

Creating User-Defined Functions:

UDFs are created using the CREATE FUNCTION statement, specifying the function name, parameters, return type, and the function body. For instance, a function named 'calculate_shipping_cost' that calculates shipping costs based on weight:

CREATE FUNCTION calculate_shipping_cost (
IN weight DECIMAL(5,2)
)
RETURNS DECIMAL(10,2)
BEGIN
DECLARE shipping_cost DECIMAL(10,2);

IF weight <= 10 THEN
SET shipping_cost = 5.00;
ELSEIF weight <= 20 THEN
SET shipping_cost = 10.00;
ELSE
SET shipping_cost = 15.00;
END IF;
RETURN shipping_cost;
END;

Conclusion:

Stored procedures and functions are powerful tools in the SQL arsenal, promoting code reusability, efficiency, and security. Stored procedures encapsulate complex operations, enhancing performance and simplifying database maintenance. User-defined functions extend SQL’s capabilities, enabling customized data transformations and calculations. By mastering these tools, database professionals can elevate their SQL skills and build robust, maintainable database applications.

I hope this blog post has provided you with a comprehensive understanding of stored procedures and functions in SQL. If you have any further questions or require additional assistance, please feel free to reach out!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ashimabha Bose
Ashimabha Bose

Written by Ashimabha Bose

Senior Business Analyst | Power BI | Digital Marketer | Data Analyst | AI Enthusiast

No responses yet

Write a response