SQL - Stored Procedures Vs Functions
Exploring stored procedures and functions, as well as their respective functionalities.

Search for a command to run...
Exploring stored procedures and functions, as well as their respective functionalities.

No comments yet. Be the first to comment.
Is the local IDE dead? Sanket Sahu discusses the rise of 'vibe-coding' and how browser-native tools like RapidNative are reshaping the future of mobile app development.

OpenClaw is a powerful, self-hosted AI assistant that connects to your tools to perform actions. Explore its Gateway architecture, real-world use cases, and security precautions.

Discover how neo-brutalism is shaping 2026 design trends. See how anti-design principles can create distinct, usable, and memorable product experiences.

When code breaks a pipeline, developers have to stop working and figure out why. This blog shows how an AI agent reads the error, finds the fix, and submits it for review all on its own.

GeekyAnts built a 5-agent fraud detection pipeline that makes decisions in under 200ms — 15x cheaper than single-model systems, with full explainability built in.

GeekyAnts Tech Blog
349 posts
GeekyAnts is an AI-powered digital product engineering and consulting company helping startups, enterprises, and Fortune 500 brands build scalable, future-ready digital solutions. Since 2006, we have delivered 800+ successful projects for 550+ global clients across healthcare, BFSI, retail, logistics, education, and enterprise technology. We help businesses accelerate digital transformation through strategy, design, engineering, and AI-led innovation.
In this article, we're going to discuss the differences between stored procedures and functions. Before we begin comparing stored procedures with functions in SQL, I'd like to explain them briefly.
A stored procedure is a collection of SQL statements that are stored in a database to perform some actions (business logic) or any database-related task.
Below, I've created a stored procedure with the name GetUsers that simply queries a USERS table and retrieves all users.
DELIMITER $$
CREATE PROCEDURE `GetUsers`()
BEGIN
SELECT * FROM USERS;
END$$
DELIMITER;
Functions may take arguments, perform calculations or operations, and return the result.
Mainly there are two types of functions:
There are a lot of built-in functions available in the databases, such as count, aggregate, date, string, and so on. Some of them are listed below, along with their definitions.
MIN() -- Returns the minimum value
MAX() -- Returns the maximum value
COUNT() -- Returns the count value
SUM() -- Returns the summation value
AVG() -- Returns the average value
Below, I've created a user-defined function with the name GetUserName which takes UserID as a parameter and returns the user's name from a USERS table.
DELIMITER $$
CREATE FUNCTION `GetUsername`(UserID int)
RETURNS varchar(32)
DETERMINISTIC
BEGIN
DECLARE Username varchar(32);
SELECT NAME INTO Username FROM USERS WHERE ID = UserID;
RETURN Username;
END$$
DELIMITER;
After gaining a basic understanding of SQL stored procedures and functions, It's time to see how the two stack up in terms of the important functionalities listed below.
Stored Procedures can call functions, but functions cannot call stored procedures.
CALL GetUsers(); -- Calling a store procedure
Functions can be called inline from SELECT, UPDATE, DELETE, and INSERT queries, but stored procedures cannot.
SELECT GetUsername(12); -- Calling a user defined function with a parameter
The methods for removing stored procedures and user-defined functions are listed below.
DROP PROCEDURE `GetUsers`; -- Removing a stored procedure
DROP FUNCTION `GetUsername`; -- Removing a user defined function
Stored procedures may return multiple values but functions only return a single scalar value or a table.
Functions always return a value, whereas stored procedures may or may not.
Stored procedures support two types of parameters: Input and Output.
Functions on the other hand only support input parameters.
Stored procedures can modify the database by using ALTER, UPDATE, DELETE, etc commands.
Since functions can only use SELECT statements, they cannot change the database state.
Stored procedures allow the use of transactions, and we can write logic to roll back/commit transactions to init.
Functions cannot make use of transactions.
Stored procedures support try-catch blocks and exception handling can also be written in init.
Functions, on the other hand, do not support init.
After reading this article, you will gain a good understanding of stored procedures and functions.
In addition, an example-based approach to using these highly powerful techniques is offered, along with information on when and where to apply them.
Thank You :)