Loop through a table collection : Table of rowtype « PL SQL « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » PL SQL » Table of rowtype 
Loop through a table collection
 
SQL>
SQL> create table department
  2  dept_id       number(2),
  3    dept_name     varchar2(14),
  4    no_of_emps    varchar2(13)
  5  )
  6  /

Table created.

SQL>
SQL> INSERT INTO department VALUES (10'ACCOUNTING', 'NEW YORK');

row created.

SQL> INSERT INTO department VALUES (20'RESEARCH',   'DALLAS');

row created.

SQL> INSERT INTO department VALUES (30'SALES',      'CHICAGO');

row created.

SQL> INSERT INTO department VALUES (40'OPERATIONS', 'BOSTON');

row created.

SQL>
SQL>
SQL> SET ECHO ON
SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2      CURSOR all_depts IS
  3          SELECT *
  4          FROM department
  5          ORDER BY dept_name;
  6
  7      TYPE dept_table IS TABLE OF department%ROWTYPE;
  8
  9      depts dept_table;
 10      depts_max PLS_INTEGER;
 11      inx1 PLS_INTEGER;
 12  BEGIN
 13      depts_max := 0;
 14
 15      depts := dept_table ();
 16
 17      FOR dept IN all_depts LOOP
 18          depts_max := depts_max + 1;
 19          depts.extend;
 20          depts(depts_max).dept_id := dept.dept_id;
 21          depts(depts_max).dept_name := dept.dept_name;
 22          depts(depts_max).no_of_emps := dept.no_of_emps;
 23      END LOOP;
 24
 25      depts.extend(5,1);
 26
 27      FOR inx1 IN 1..depts_max+LOOP
 28          DBMS_OUTPUT.PUT_LINE (depts(inx1).dept_id ||' ' || depts(inx1).dept_name);
 29      END LOOP;
 30
 31      depts.trim(5);
 32
 33      depts.delete(1);
 34
 35      DBMS_OUTPUT.PUT_LINE(depts.count);
 36
 37      FOR inx1 IN 1..depts_max+LOOP
 38          IF depts.exists(inx1THEN
 39              DBMS_OUTPUT.PUT_LINE (
 40                  depts(inx1).dept_id ||
 41                  ' ' || depts(inx1).dept_name);
 42          END IF;
 43      END LOOP;
 44
 45  END;
 46  /
10 ACCOUNTING
40 OPERATIONS
20 RESEARCH
30 SALES
10 ACCOUNTING
10 ACCOUNTING
10 ACCOUNTING
10 ACCOUNTING
10 ACCOUNTING
3
40 OPERATIONS
20 RESEARCH
30 SALES

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> drop table department;

Table dropped.

SQL> --

 
Related examples in the same category
1. Use table of rowtype to define data type
2. table of employees%rowtype index by binary_integer
3. Declare an index-by table variable to hold the employee records in cursor
4. Close a nested table type
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.