How to use PL/SQL Cursors In SQL

Using PL/SQL cursors are an effective method for the retrieving the data from database and it process it row by row. Cursors are allowed to the iterate through the result set returned by the SQL query and enabling to the perform operations on each row individually.

Step 1: Create a table and name it as employee.

Here is the employee table.

Employee table

Step 2: Implement the Code

DECLARE
v_total_salary NUMBER := 0;
BEGIN
FOR emp_record IN (SELECT * FROM employee) LOOP
v_total_salary := v_total_salary + emp_record.salary;
END LOOP;

DBMS_OUTPUT.PUT_LINE('Total Salary: ' || v_total_salary);
END;
/

Output:

Output

Explanation:

  • We declare the variable v_total_salary to store the total salary is initialized to 0.
  • We can use cursor loop to the iterate the each row of the employee table. The cursor emp_record holds the current row data.
  • In the loop, we increment the v_total_salary by adding the salary of the employee.
  • Once the loop is finished, we print the total salary with the help of DBMS_OUTPUT.PUT_LINE.

How to Get Counts of all Tables in a Schema in PL/SQL?

In Database Management System, it is essential to retrieve the statistical information about tables with the schema. Whether it is for monitoring the database health, optimizing the performance, or simply understanding the data structures having access to row counts of the tables can be more valuable.

Similar Reads

How to Get Counts of all Tables in a Schema in PL/SQL?

In database management obtaining insights into the structure and the content of the database tables is crucial for various tasks like performance optimization, resource management, and data analysis. One of the fundamental aspects of this process is determining the number of rows within each table of the schema....

Using PL/SQL Cursors

Using PL/SQL cursors are an effective method for the retrieving the data from database and it process it row by row. Cursors are allowed to the iterate through the result set returned by the SQL query and enabling to the perform operations on each row individually....

Using PL/SQL Procedures

Using the PL/SQL procedure is the structured way to the encapsulate the series of SQL and PL/SQL statements into reusable units of the code. Procedures can be accept the input parameters, perform the operations and it returns output values. These are mainly useful for the modularizing the code, improving the maintainability and promoting the code reuse....

Conclusion

Overall, using PL/SQL for retrieve the row counts for all the tables in a schema provided the powerful and efficient way to the gather statistical information about the database tables. By dynamically querying system tables and execute the dynamic SQL statements, we will automate the process of the counting rows in the each table. This approach is particularly used for the tasks like monitoring database health and identifying the performance bottlenecks and understanding the data distribution in the database schema. By incorporating the PL/SQL-based solutions into the database management workflows and organisations can be streamline monitoring, optimizing and decision making process. To retrieve the counts of all the tables in a schema enhances effectiveness and efficiency of the database administration and tasks of the development and it contributing the overall success of database-driven applications and systems....