What is the difference between function and procedure in PL/SQL?

后端 未结 7 1044
日久生厌
日久生厌 2020-11-27 14:30

What is the difference between function and procedure in PL/SQL ?

7条回答
  •  隐瞒了意图╮
    2020-11-27 14:49

    In dead simple way it makes this meaning.

    Functions :

    These subprograms return a single value; mainly used to compute and return a value.

    Procedure :

    These subprograms do not return a value directly; mainly used to perform an action.

    Example Program:

    CREATE OR REPLACE PROCEDURE greetings
    
    BEGIN 
    
    dbms_output.put_line('Hello World!');
    
    END ;
    /
    

    Executing a Standalone Procedure :

    A standalone procedure can be called in two ways:

    • Using the EXECUTE keyword • Calling the name of procedure from a PL/SQL block

    The procedure can also be called from another PL/SQL block:

    BEGIN 
    greetings;
    END;
    /
    

    Function:

    CREATE OR REPLACE FUNCTION totalEmployees 
    RETURN number IS
    total number(3) := 0;
    BEGIN 
    SELECT count(*) into total 
    FROM employees;
    RETURN total; 
    END;
    /
    

    Following program calls the function totalCustomers from an another block

    DECLARE 
    c number(3);
    BEGIN 
    c := totalEmployees();
    dbms_output.put_line('Total no. of Employees: ' || c);
    END;
    /
    

提交回复
热议问题