Display total number of rows in a table

Consider the following tables to complete the following assignment.

Table 1:  'Emp_Detail'

Employee_idFirst_NameLast_nameSalaryDEPT_ID
1ShrutiShrabya500001
2JayaSingh100002
3MangalaThokal600003
4SurendraMaurya700004

Table2: 'Department'

Dept_IDDept_NameManager_ID
1Accounting1
2Shipping3
3Store3

Q. Write a PL/SQL block to create a cursor based records and display the total number of rows present in the table.

Answer:

The structure of cursor based records is similar to elements of a predefined cursor. The following code creates a cursor based records.
So, the following code creates a cursor based record and finds a total number of rows and exits when no rows found to fetch.

DECLARE
CURSOR c_emp_detail IS
   SELECT employee_id,first_name,last_name,salary
   FROM emp_detail;
   rec_emp_detail c_emp_detail%ROWTYPE;   
BEGIN
OPEN c_emp_detail;
   LOOP
    FETCH c_emp_detail INTO rec_emp_detail;
    EXIT WHEN c_emp_detail%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE('Employees Details : '||' '||rec_emp_detail.employee_id ||' '||rec_emp_detail.first_name||' '||rec_emp_detail.last_name);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Total number of rows : '||c_emp_detail%ROWCOUNT);    
CLOSE c_emp_detail;
END;


The above code will display the total number of selected fields and shows the total number of rows present in the table.

Output:

cursor based records