What is the difference between "Stored Procedure" and "Function"?

5/3/2022

SQL performance optimization with Functions and Procedures

Go Back

Difference Between Stored Procedure and Function in SQL

What is a Stored Procedure?

A stored procedure is a prepared SQL code that can be saved and reused multiple times. Instead of writing the same query repeatedly, you can store it as a procedure and call it whenever required.

Syntax of a Stored Procedure:

CREATE OR REPLACE PROCEDURE <procedure_name>
    (<parameter IN/OUT <datatype>>)
[ IS | AS ]
    <declaration_part>
BEGIN
    <execution_part>
EXCEPTION
    <exception_handling_part>
END;

Example of a Stored Procedure:

CREATE PROCEDURE SelectAllDeveloper
AS
SELECT * FROM developer
GO;
SQL performance optimization with Functions and Procedures

What is a Function?

A function in SQL is a standalone PL/SQL subprogram that performs a specific task. Similar to stored procedures, functions have unique names by which they can be referred. Functions are often used for calculations, data processing, and returning single values.

Syntax of a Function:

CREATE OR REPLACE FUNCTION <function_name>
    (<parameter IN/OUT <datatype>>)
RETURN <datatype>
[ IS | AS ]
    <declaration_part>
BEGIN
    <execution_part>
EXCEPTION
    <exception_handling_part>
END;

Key Differences Between Stored Procedures and Functions:

Feature Stored Procedure Function
Return Type Can return multiple values using OUT parameters Must return a single value
Usage in SQL Queries Cannot be called directly in SELECT statements Can be used in SELECT statements
Error Handling Uses EXCEPTION block Uses EXCEPTION block but is more limited
Execution Executed using CALL or EXEC command Called directly in SQL queries
Modifications Can modify database tables (DML operations) Cannot modify database tables
Reusability Used for complex operations involving multiple steps Used for calculations and returning values

Below is point of difference between stored Procedure and Function

1. A procedure  contain both input and output parameters, but a function can contain only
 input parameters .
2. Inside a procedure we can use DML Data Manipulation Language (INSERT/UPDATE/DELETE) statements. But inside a function we can't use DML statements.
3. We can't utilize a Stored Procedure in a Select statement. But we can use a function
in a Select statement.
4. We can use a Try-Catch Block in a Stored Procedure but inside a function we can't
use a Try-Catch block for debuging purpose .
5. We can use transaction management in a procedure code but we can't in a function  code.
6. We can't join a Stored Procedure for any use case but we can join functions.
7. Stored Procedures cannot be used in the SQL statements anywhere in the
WHERE/HAVING/SELECT section. But we can use a function anywhere in our Query.
8. A procedure code can return 0 or n values (max 1024). But a function code can return only 1
value that is mandatory .
9. A procedure can't be called from a function but we can able to  call a function from a
procedure in Query.

Frequently asked questions (FAQ) in "Stored Procedure" and "Function"

  1. difference between stored procedure and function
  2. difference between stored procedure and function in oracle
  3. difference between stored procedure and function in c#
  4. difference between stored procedure and function in postgresql
  5. difference between stored procedure and function mysql
  6. difference between stored procedure and functions in sql server
  7. difference between stored procedure and function and view in sql server

Conclusion:

In this article ,We can choice between a stored procedure and a function depends on the specific requirements of the task and both are important in SQL.
It's often a good practice to take a backup before performing such operations, especially in a production environment.
We provided SQL  Interview Question for learning.

Stored procedures and functions are both powerful features in SQL that help optimize query execution and improve code reusability. While stored procedures are used for complex operations that may involve multiple queries and updates, functions are mainly used for calculations and returning specific values. Choosing between the two depends on the use case and requirements of your SQL operations.

Table of content