A subquery in the FROM clause of a cursor within a cursor FOR loop : CURSOR FOR Loop « Cursor « 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 » Cursor » CURSOR FOR Loop 
A subquery in the FROM clause of a cursor within a cursor FOR loop
 

SQL> -- create demo table
SQL> create table Employee(
  2    empno              Number(3)  NOT NULL, -- Employee ID
  3    ename              VARCHAR2(10 BYTE),   -- Employee Name
  4    hireDate          DATE,                -- Date Employee Hired
  5    orig_salary        Number(8,2),         -- Orignal Salary
  6    curr_salary        Number(8,2),         -- Current Salary
  7    region             VARCHAR2(BYTE)     -- Region where employeed
  8  )
  9  /

Table created.

SQL>
SQL> -- prepare data for employee table
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(122,'Alison',to_date('19960321','YYYYMMDD'), 45000,       48000,       'E')
  3  /

row created.

SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(123'James',to_date('19781212','YYYYMMDD'), 23000,       32000,       'W')
  3  /

row created.

SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(104,'Celia',to_date('19821024','YYYYMMDD'), 53000,       58000,        'E')
  3  /

row created.

SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(105,'Robert',to_date('19840115','YYYYMMDD'), 31000,      36000,        'W')
  3  /

row created.

SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(116,'Linda', to_date('19870730','YYYYMMDD'), 43000,       53000,       'E')
  3  /

row created.

SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(117,'David', to_date('19901231','YYYYMMDD'), 78000,       85000,       'W')
  3  /

row created.

SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(108,'Jode',  to_date('19960917','YYYYMMDD'), 21000,       29000,       'E')
  3  /

row created.

SQL>
SQL>
SQL> create table job(
  2    empno              Number(3)  NOT NULL, -- Employee ID
  3    jobtitle           VARCHAR2(10 BYTE)    -- Employee job title
  4  )
  5  /

Table created.

SQL>
SQL> -- prepare data for job table
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(101,   'Painter');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(122,   'Tester');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(123,   'Dediator');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(104,   'Chemist');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(105,   'Accountant');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(116,   'Manager');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(117,   'Programmer');

row created.

SQL>
SQL> insert into job(empno, jobtitle)
  2           values(108,   'Developer');

row created.

SQL>
SQL>
SQL> -- display data in the table
SQL> select from Employee
  2  /

     EMPNO ENAME      HIREDATE  ORIG_SALARY CURR_SALARY R
---------- ---------- --------- ----------- ----------- -
       122 Alison     21-MAR-96       45000       48000 E
       123 James      12-DEC-78       23000       32000 W
       104 Celia      24-OCT-82       53000       58000 E
       105 Robert     15-JAN-84       31000       36000 W
       116 Linda      30-JUL-87       43000       53000 E
       117 David      31-DEC-90       78000       85000 W
       108 Jode       17-SEP-96       21000       29000 E

rows selected.

SQL>
SQL> select from job;

     EMPNO JOBTITLE
---------- ----------
       101 Painter
       122 Tester
       123 Dediator
       104 Chemist
       105 Accountant
       116 Manager
       117 Programmer
       108 Developer

rows selected.

SQL>
SQL>
SQL>
SQL>
SQL> -- Cursors with subqueries
SQL>
SQL> -- A subquery in the FROM clause of a cursor within a cursor FOR loop:
SQL>
SQL> BEGIN
  2     /* The FROM Clause of the SELECT statement contains a
  3        subquery that select the total number of employees
  4        enrolled in each dept */
  5
  6     FOR job_record IN
  7          (SELECT jobtitle FROM job where empno = (SELECT empno FROM employee where ename='James')
  8           )
  9     LOOP
 10
 11       DBMS_OUTPUT.PUT_LINE(job_record.jobtitle);
 12     END LOOP;
 13  END;
 14  /
Dediator

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>
SQL>
SQL> drop table job;

Table dropped.

SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /

Table dropped.

SQL>
SQL>
SQL>

           
         
  
Related examples in the same category
1. Create a cursor in for statement
2. The %FOUND, %NOTFOUND, and %ROWCOUNT cursor attributes can be used to guide a loop
3. A function that uses a CURSOR FOR Loop
4. Use 'for loop' to loop through the cursor
5. Assign value from cursor to a table collection by index
6. Calculation based on cursor value
7. For each row in a cursor
8. Output value in cursor one by one
9. Simple Loop and fetch cursor
10. Use pl/sql, cursor and for loop to count table
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.