Go Back

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

5/3/2022
All Articles

#sql #sqlCommand #Stored Procedure #sql_Function

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

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

Definition of stored Procedure :

Prepared SQL code that you may save and reuse repeatedly is called a stored procedure. To avoid writing the same SQL query twice, consider saving it as a stored procedure that can be called whenever needed.

Syntax and example :

CREATE OR REPLACE PROCEDURE
<procedure_name>
    (
    <parameterl IN/OUT <datatype>
    )
[ IS | AS ]
    <declaration_part>
BEGIN
    <execution part>
EXCEPTION
    <exception handling part>
END;

 

CREATE PROCEDURE SelectAlldeveloper
AS
SELECT * FROM developer
GO;

Definition of Function :

Functions is a standalone PL/SQL subprogram. Like PL/SQL procedure, functions have create with unique name by which it can be referred. These are stored as PL/SQL database objects.
we can also analysis in PL/SQL , It contains various built-in functions to work with strings and date datatype. Here we are going to see the commonly used functions and their usage.

CREATE OR REPLACE FUNCTION
<procedure_name>
(
<parameterl IN/OUT <datatype>
)
RETURN <datatype>
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;

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.

 

Article