iSelfSchooling.com - Copyright © 1999-2007 iSelfSchooling.com  References  Job Openings  |  Secure Login
    Home  | Search more...  |  FREE Online VIDEO Oracle Training  |  Gift Store  |  Bookstore

   Unlimited access!   

    Oracle  Syntax  | Suggestions Your Contribution  |  FREE Legal Forms

 

Email2aFriend Homepage us! |  Bookmark   -  Copyright & User Agreement

Products/Services

 Vision/Mission

 Community Sharing

 Services

  Products

 Biography

 Contact Us

 FAQ

 Current News

 Website Traffic

 Bookstore

 FREE Training

 SQL

 PL/SQL

 Forms 

 Reports

 Other TOOLS

 Fundamentals

 Performance

 OEM

 Application Server

 Grid Control

 Articles

 Prepare for OCP

Oracle SYNTAX

 Oracle Functions

 Oracle Syntax

 Oracle 10g Syntax

  PL/SQL Syntax

UNIX and more...

 UNIX for DBAs

 LINUX for DBAs

 DB using PHP

  A+ Certification

 Basics of JAVA  

 Tips of  SEO

Finance/Jobs

 Financial Aid

 Skilled

 Oracle

 Jobs

  Magazine

More Training

 Q & Answers

 SQL-PL/SQL

 DBA

 Developer

 Important Notes

 Case Studies

 9i New Features

 10g New Features

 10g Qs/As

 Grid Control

 OracleAS # I

 OracleAS # II

  LDAP and OID

  HTTP Server

 Instructor-Led

  Virtual Hosts

 Community Sharing

More to know...

Acknowledgement**

 FREE Legal Forms

 Who is who

 Market Place

 University Directory

 Advisory Articles

 Links...

 

 

FREE Online Oracle Training for beginners and advanced - The most comprehensive Oracle tutorial

The authors do not guarantee or take any responsibility for the accuracy, or completeness of the information.

BASICS

SQL | PL/SQL

DEVELOPERS

FORMS 2 | REPORTS | Other TOOLS

DBAs

FUNDAMENTALS 2 | PERFORMANCE | OEM

ADVANCE

APPLICATION SERVER | GRID CONTROL | ARTICLES 2 3 4

Basics - PL/SQL 

Lesson 01 | Lesson 02 | Lesson 03 | Lesson 04 | Lesson 05 | Lesson 06 | Lesson 07 | Lesson 08 | Lesson 09 | Lesson 10 | Lesson 11 | Lesson 12 | Lesson 13 | Lesson 14 | Lesson 15 | Lesson 16 | Lesson 17 | Lesson 18 | Lesson 19 | Lesson 20 | Lesson 21 | Lesson 22 | Lesson 23 | Lesson 24 | Lesson 25 | Lesson 26 | Lesson 27 | Lesson 28 | Lesson 29 | Lesson 30 | Lesson 31 | Lesson 32 | Lesson 33 | Lesson 34 | Lesson 35 |

Lesson 02

“Don't walk behind me, I may not lead. Don't walk in front of me, I may not follow. Just walk beside me and be my friend.” Albert Camus (1913 - 1960) (attributed)

Read first then play the video:

   PLS003(VIDEO)-Explicit Cursor Handling

 

Explicit Cursor Handling

Hands-On Introduction

In this Hands-On, you will declare a cursor to list the department name (dname), total number of employees (ttemp), total salary (ttsal), and average salary (avsal) for each department from the department table and employee table.

 

Then, you print all department name with their total number of employees (For example: ACCOUNTING has 3 employees) for each department. You use the notepad editor.

 

Go to “MS-DOS.” Change directory to the iself directory. And login to “sqlplus” as "iself/schooling."

 

Declare Variables (Naming convention)

Open the notepad editor, write a PL/SQL block to print all the department names with their total number of employees. Declare a record type to have four items: Department name, total number of employees, total salary, and average salary. Follow the naming convention to start a type name with "t_", a variable name with "v_", a cursor name with "c_", and a parameter name with "p_".

 

Declare Explicit Cursor

The explicit cursor will be defined in the declaration section. Once you define an explicit cursor, you should open it the PL/SQL body, fetch the cursor one at a time, exit from the loop if you have one, and at the end close the cursor.

Declare a cursor to list the department name, total number of employees, total salary, and average salary from the department and employee table order by the department name.

 

OPEN, FETCH, LOOP, and EXIT a Cursor (Simple loop)

In the body or execution section, open the cursor. Make a simple loop. In the loop, read a record one at a time using fetch statement. Make sure to exit from the loop. Use the "dbms_output" package to print the department name and their total number of employees. End the loop and then close the cursor. Make it easy to read.

(Notepad)

>> DECLARE

-- Declare a variable for a cursor.

TYPE t_ds IS RECORD (

dname dept.dname%type,

ttemp number(3),

ttsal number(8,2),

avsal number(8,2));

 

-- define department statistics

CURSOR c_ds is

select dname, count (*) ttemp,

sum(sal) ttsal, avg(sal) avsal

from dept d, emp e

where d.deptno = e.deptno

group by dname

order by 1;

 

-- define a variable for cursor

v_ds t_ds;

BEGIN

-- open the cursor

OPEN c_ds;

-- start loop

LOOP

--read a record

FETCH c_ds INTO v_ds;

-- exit from loop

EXIT WHEN c_ds%notfound;

-- list dept. name

dbms_output.put_line

(v_ds.dname ||

‘ has ’ || v_ds.ttemp || ‘ employees.’);

-- end the loop

END LOOP;

CLOSE c_ds;

END;

/

Save a PL/SQL block

Save the file in the "iself" directory as "test_fetch_cursor.sql."

Go to “SQLPLUS.” Get the file. Compile and run the PL/SQL block.

SQL> get c: test_fetch_cursor.sql

SQL> /

You should not have any error messages in the compilation. If you have try to correct your errors before to get to next step.

 

Run a PL/SQL block

Set the serveroutput to on. Then run the file.

SQL> set serveroutput on

SQL> @test_fetch_cursor

 

 

 

“The first step to getting the things you want out of life is this: Decide what you want.” Ben Stein

Questions:

Q: Describe that why do we need to use a solid naming convention in our PL/SQL program.

Q: What is the explicit cursor in the PL/SQL language?

Q: What are the differences between the explicit and implicit cursors?

Q: Where do you declare an explicit cursor in the PL/SQL language?

Q: Where do you declare an implicit cursor in the PL/SQL language?

Q: What is a simple loop in the PL/SQL language?

Q: How do you open an explicit cursor in the PL/SQL language?

Q: What does the FETCH statement in the Oracle PL/SQL language?

Q: How do you terminate from a simple loop in the PL/SQL language?

Q: How do you OPEN or CLOSE a cursor in the PL/SQL language?

Q: Declare a cursor to list the department name (dname), total number of employees (ttemp), total salary (ttsal), and average salary (avsal) for each department from the department table and employee table order by the department name.

Write all department name with their total number of employees for each department using the notepad editor.

For example: ACCOUNTING has 3 employees.

(Note: Don’t use the ttemp, ttsal, and avsal item at this time)

 

 

 
 
Google
 
Web web site