Skip to main content

Stored Function in Oracle Database and it’s benefit #Diksha Oracle Training Centre

              Stored Function in Oracle Database and it’s benefit.


Hello Everyone,

In my previous blogs we discussed about stored procedures and its attributes and benefits of using Stored Procedure in Databases.Today we will discuss about Stored Function. Stored Function is also named block like Stored Procedure a stored function is a set of PL/SQL statements you can call by name. Stored functions are very similar to procedures, except that a function returns a value. Stored functions must return a value whether condition is true or false, but in Stored procedure value should return only when condition is true.  Stored function only return one value, whereas in stored procedure if condition is true it can give you multiple record as well as multiple transaction. Stored Function can be used in Procedures, Packages as well as into select statement, but procedure can’t be called in select statement. Both Stored Procedures and functions are stored in a compact compiled form. When called, they are loaded and processed immediately. Subprograms take advantage of shared memory, so that only one copy of a subprogram is loaded into memory for execution by multiple users. Procedures and functions defined within a package are known as packaged subprogram.

Creation of Stored Function is Similar to Stored Procedure where we can create , modify , run and drop with SQL command similar to what we use in Stored Procedure , only difference in stored function is we need to put RETURN data type in CREATE statement while creating the Stored function . The value returned by the function can be used directly in the DBMS_OUTPUT.PUT_LINE Statement.

Before we go to Syntax and example of Stored function , Please check my previous blogs for Stored procedures to understand easily as in Stored function we use same arguments like IN , OUT and attributes like %TYPE , %ROWTPE , Cursor etc. All this important parts mentioned in below blogs.

Syntax for Stored Function is as below

CREATE OR REPLCE FUNCTION function_name

   RETURN datatype

   IS variable name data type

DECLARE

   <declarations section>

BEGIN

   <executable command(s)>

EXCEPTION

   <exception handling>

RETURN variable name

END;

 

Here you see it’s very similar block structure as we use in Stored Procedure only thing we need to include RETURN data type in CREATE statement while creating the Stored function . The value returned by the function can be used directly in the DBMS_OUTPUT.PUT_LINE Statement by Execute command or we can also call this in SQL statement:

Simple Stored Function Example where we pass emp_id and get the salary:

CREATE OR REPLACE FUNCTION get_salary(p_empid IN NUMBER)

   RETURN NUMBER

   IS v_salary NUMBER;

   BEGIN

      SELECT salary

      INTO v_salary

      FROM employee

      WHERE emp_id = p_empid;

      RETURN(v_salary);

    END;

/

 

Here the get_salary function returns the salary of a specified emp_id given by user.When you call the function, you must specify the argument account_no as we put it in IN parameter, The datatype of emp_id is NUMBER. The function returns the salary for particular employee id passed by user. The RETURN clause of the CREATE FUNCTION statement specifies the datatype of the return value to be NUMBER.

 

The function created above can be used in a SQL statement.

Example:

SELECT get_salary(10002) FROM DUAL;

This will give you one result as Function return only one value;

It will give you the salary for that particular employee id. This is the main benefits of Stored  function is that  we can it in select statement but Stored Procedure cannot be called in Select statement.

 

Another way to execute Stored Function

DECLARE

    l_salary NUMBER;

BEGIN

    l_salary := get_salary(2000);

    DBMS_OUTPUT.PUT_LINE('Salary of given employee id is '|| l_salary);

END;

This statement will also give you one result as Function return only one value.

Output of the above Stored Function is as below:



Removing Function we use Drop Statement as below

DROP FUNCTION get_salary;


Another Example of Stored Function:

Creating function  to get_address from employee and employee_details table against empid

 

Create or replace FUNCTION get_address(v_empid IN NUMBER)

   RETURN VARCHAR2

   IS address_details VARCHAR2(160);

   BEGIN

 SELECT a.name||' '|| a.address||' '|| b.city ||' '||b.state||' '||b.country

  INTO address_details

  FROM employee a , employee_details b

  WHERE a.emp_id = b.emp_id

   AND a.emp_id = v_empid;

      RETURN(address_details);

    END get_address;

/

 

In above stored function we pass v_empid  as IN Parameter ,We can call this function by using both select statements as well as call it from dbms_output.put_line  and also by using EXECUTE command.

SELECT get_address(2000) FROM DUAL;

This will give you only 1 output against the emp_id

Another way to execute this function

DECLARE

    l_adress varchar2(160);

BEGIN

    l_adress := get_address(2000);

    DBMS_OUTPUT.PUT_LINE('Details are '|| l_adress);

END;

This will also give you one result as function will give you only 1 result.

Output of above Stored Function is as below:


Removing Function we use Drop Statement as below

DROP FUNCTION get_address;

Now we are able to understand Stored Function must return a value and can be used in select statement or we can execute Stored Function by execute statement.


Note: We can call Stored Function in Oracle packages as we can use it for reusability as it stored in database same like stored Procedures.

·       The function must return a value but in Stored Procedure it is optional. Even a procedure can return zero or n values.

·        Functions can have only input parameters for it whereas Procedures can have input or output parameters.

·        Functions can be called from Procedure whereas Procedures cannot be called from a Function.

As you all understand both Stored Procedure and Stored function, Please try to write both stored Procedures and Functions as much as you can so that it’s easy for you to understand Oracle Package where we can use both Procedures and functions.

Please read this blog carefully and let me know if you have any doubts on creating stored function. Also in interview lots of question will be asked from Stored Procedures and Functions. So you can explain easily now with some practical examples as you seen some differences and similarities between Stored Procedure and Stored Function. In next Blog I will come up with some more examples and differences between them.

 

 Thanks

Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...