Functions in PL/SQL

 Function in PL/SQL:

 Functions are similar to Procedure except Function can return a value using   RETURN keyword.

Syntax:

Create [OR Replace] Function Function_name

[(Parameter_Name [IN | OUT |IN OUT] type [,…])]

Return return_datatype

IS | As

    [ Declaration statements ]         

Begin

    [ Executable statements ]

  Return variable_name;    

Exception

  [ Exception handler ]             

End Function_name;

 

Syntax Explanation:

 ·       A Function is created with the Create [OR Replace] Function statement.

·        ·    RETURN keyword is used to return a value

Example:

This example for finding total number of customers in the customer table:

CREATE OR REPLACE FUNCTION total_Customers

RETURN number

IS

   total number := 0;

BEGIN

   SELECT count(*) into total

   FROM hz_cust_accounts;

   

   RETURN total;

END;

 


Execution method:

 DECLARE

   cust number(2);

BEGIN

   cust:= total_Customers();

   dbms_output.put_line('Total no.of Customers: ' || cust);

END;


Following Function used to return the employee name in the particular

Department using cursor.

CREATE OR REPLACE FUNCTION Find_emp (p_deptno IN NUMBER)

   RETURN VARCHAR2

IS

   v_ename   VARCHAR2 (2000);

 

   CURSOR c_cur

   IS

      SELECT deptno

        FROM dept

       WHERE deptno = p_deptno;

BEGIN

   FOR v_cur IN c_cur

   LOOP

      BEGIN

         SELECT LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename)

           INTO v_ename

           FROM emp

          WHERE emp.deptno = v_cur.deptno;

 

         DBMS_OUTPUT.put_line (   'deptno='

                               || v_cur.deptno

                               ||' '

                               || 'emp name ='

                               || v_ename

                              );

EXCEPTION

         WHEN NO_DATA_FOUND

         THEN

            RETURN v_ename;

         WHEN TOO_MANY_ROWS

         THEN

            DBMS_OUTPUT.put_line (   'dept no='

                                  || v_cur.deptno

                                  ||' '

                                  || ',emp name :'

                                  || v_ename

                                 );

      END;

   END LOOP;

 

   RETURN v_ename;

 

END Find_emp;

 

 In his program to separate the customer name in comma  (,) separator using

 LISTAGG clause.

LISTAGG :

LISTAGG (<expression>,<separator> WITHIN GROUP(order by ...) 

Execution method:

DECLARE

   v_result   VARCHAR2(4000);

BEGIN

   v_result := xxdtj_emp (p_deptno => 10 );

   DBMS_OUTPUT.put_line ('Return :' || v_result);

END;

Drop Function:

 

Drop function function_name;

 

Example:

 

Drop function find_emp;

 

 

 

 

 

Share:

REP-1204: Repeating frame 'repeating_frame_name' references invalid group.

Error in report builder:


Reasons for populate “REP-1204: Repeating frame 'repeating_frame_name' references invalid group” error:
  • If the report of repeating frame source name is null then this error populate.
  • For example, see the below screen short


Solution:
  • Give repeating source name as group name like data module of the group name.
  • For the above example, the report having the group name G_DEPTNO

  • Open the property repeating frame of the pallet.

  • After changing the repeating source name.
Share:

PL/SQL procedures

Procedure in PL/SQL:
·       Procedures are used to perform a particular task. The procedure is the subprogram which is combined to perform large programs.
·       These subprograms do not return a value directly, return can be done by using OUT parameters.
·       Procedures are mainly used to perform an Action.
Difference between Procedure and function:
Procedures 
Functions
Procedures are used to perform
Particular action.
Functions are used to perform
Calculation.
These subprograms do not
Return a value directly.
These subprograms are a Return
A value using the RETURN keyword.

Syntax:

    Create [OR Replace] procedure Procedure_name
      [(Parameter_Name [IN | OUT |IN OUT] type [,…])]
        {Is | As }
            [ Declaration statements ]          /* Optional*/
            Begin
                [ Executable statements ]        /* Mandatory */
                Exception
                   [ Exception handler ]              /* Optional*/
                    End procedure_name;
                    Syntax Explanation:
                    A procedure is created with the CREATE OR REPLACE PROCEDURE statement.
                    Declaration Section:
                    • It is an optional part. The declaration part does not start with DECLARE keyword. 
                    • It contains a declaration of variables, constants, cursors, exceptions....etc.
                    Executable Section:
                    • It is a mandatory part.
                    • The executable part is used to perform the particular designated action or task.
                    Exception Handling:
                    • It is an optional part. 
                    • The executable part is used to perform the particular designated action or task.
                     Example:
                    1. Create or Replace procedure Ex_p1
                      As
                      Begin
                       Dbms_Output.Put_Line(‘ This is the First Example’);
                      End;
                    Following Procedure for delete, insert, update on Emp table using parameters:
                    1. CREATE OR REPLACE PROCEDURE xxdtj_update_emp (
                         p_empno    IN   emp.empno%TYPE,
                         p_ename    IN   emp.ename%TYPE,
                         p_job      IN   emp.job%TYPE,
                         p_mgr      IN   emp.mgr%TYPE,
                         p_sal      IN   emp.sal%TYPE,
                         p_deptno   IN   emp.deptno%TYPE
                      )
                      IS
                      BEGIN
                         DELETE FROM emp
                               WHERE emp.empno = p_empno;
                              IF SQL%FOUND
                             THEN
                                 Dbms_Output.Put_Line ('Delete succeeded for empno: ' ||         p_empno);
                             INSERT INTO emp
                                        (ename, job, mgr, sal, deptno
                                        )
                                 VALUES (p_ename, p_job, p_mgr, p_sal, p_deptno
                                        );
                             ELSE
                            Dbms_Output.Put_Line ('no emp of no ' || p_empno || 'is found.');
                            Dbms_Output.Put_Line ('The emp of no is not found so' || p_empno || 'is added into the emp table.');
                                    UPDATE emp
                                   SET ename = 'xxx'
                                   WHERE empno = p_empno;
                           END IF;
                      END;
                    Execution Method:
                    Method 1:
                    BEGIN
                       xxdtj_update_emp(1001);
                    END;
                    Method 2:
                    Begin
                    xxdtj_update_emp (p_ename     => 'sana'
                                                    p_job           => 'ADMIN',
                                                      p_mgr           =>  123,
                                                      p_sal             =>  20000
                                                    p_deptno       =>  20);
                    End; 

                    Drop Procedure:

                     Drop procedure procedure_name;

                    Share:

                    PL/SQL Introduction

                    PL/SQL Introduction:

                    •  PL/SQL is the procedural language which is used in oracle platform. 
                    •  PL/SQL enables program or Application developers to combine SQL with procedural statements.

                     PL/SQL Block contains four sections,

                       1. Declare(Optional)

                       2. Begin(Mandatory)

                       3. Exception(Optional)

                       4. End(Mandatory)

                    Syntax:              

                    Declare                        

                          Declaration statements ;

                     Begin

                          Executable statements ;

                     Exception

                         Exception handling statements ;

                     End;

                     


                    Example
                    :     

                    Declare

                        v_count number;

                     Begin

                           select count(*) into v_count

                           from emp;

                     Exception

                          when SQL%Notfound then

                                Dbms_Output.Put_line('No data found');

                     End;

                     


                    Explanation
                    :

                    Declare:

                    ·       Declare section starts with DECLARE keyword in which constants, variables, cursors can be declared which stores data temporarily.

                    ·       This Declare section is optional.

                    Execution:

                    ·       Execution section starts with BEGIN and ends with END Keyword. program logic to be written here to perform any task, it support all

                    ·       DDL, DML commands ,condition expressions etc.This section is Mandatory.

                    Exception:

                    ·       It starts with Exception keyword. which is used to handle the run time errors in the executable section. This section is optional.

                    PL/SQL Main concepts:

                      1. Procedures

                      2. Functions

                      3. Packages

                      4. Triggers

                      5. Cursors

                      6. Exception handling

                     

                     


                    Share:

                    REP-1213: Field '' references column '' at a frequency below its group.

                    Error in report builder:

                     

                    Reasons for populate “REP-1213: Field '<field name>' references column '<column name>' at a frequency below its group” error:
                    • If your query getting too many rows then we got this error. See the below screen short,


                    Solution:
                    • Repeating frame added into the mainframe and the repeating frame must be having all fields in similar to data molding.
                    • For more details, see the below screen short,


                    Another possible:
                    • If already you have the repeating frame but that field not in repeating frame means this error also came.
                    • The source column for the field is in a group below that of the repeating frame which encloses it.
                    •  Action: Move the field into a repeating frame whose source group is at or above the group that contains the field's source column. Otherwise, move the field's source column into the enclosing repeating frame's group, if possible.

                    Share:

                    FRM-32083: Value length is too long for maximum length of item.

                    Error in the oracle form builder:


                    Reason for populate “FRM-32083: Value length is too long for the maximum length of item.” error:
                    • For example, one time of the maximum length is 3 but I used this item from anywhere but I give different size means this error will be come. 
                    • See the below screenshot will helpful for you.


                    Solution:
                    • Item of maximum length must be equal to declaring variable length from anywhere.


                    Share:

                    Oracle Application unable to open because of java installation version does not support in your browser.

                     Error: 
                     Oracle Application unable to open because of java installation version does not support in your browser.
                    Solution:
                    While opening the oracle forms using firefox or any other browser, it shows java installation error then just uninstall your browser and then again install it.
                     Next, go to the control panel check the  'Never check for updates' must be checked.
                     And then Must Uncheck the 'use a background service to install updates'.
                     

                    Share:

                    Recent Posts

                    Service Support

                    Need our help to Learn or Post New Concepts Contact me