Advanced Database
Introduction to PL/SQL
Lec 5
Mohammed
1
Outlines
• Control Statements
• Cursors
2
Conditional Controls
• Conditional Controls
• IF....THEN....END IF;
• IF....THEN...ELSE....END IF;
• IF....THEN...ELSIF....THEN....ELSE....END IF;
3
Loops
• A loop statement allows us to execute a
statement or group of statements multiple
times.
• Loops
– Simple Loops
– WHILE Loops
– FOR Loops
4
PL/SQL Loops
• When the number of iterations is unknown
– Simple loops: executes at least once
– WHILE loops: executes while the condition is true
• When the number of iterations is known in advance
– Numeric FOR Loops: executes a specific number of times
SIMPLE LOOP
LOOP
sequence_of_statements;
EXIT WHEN condition;
END LOOP;
LOOP
DBMS_OUTPUT.PUT_LINE ('mohammed');
EXIT WHEN i > 100;
END LOOP;
5
For Loops
The FOR-LOOP statement lets you specify a range of integers, then execute a
sequence of statements once for each integer in the range.
FOR LOOP
FOR loop_Counter IN IN [REVERSE]
low..high LOOP
sequence_of_statements;
END LOOP;
FOR i IN 1..order_qty LOOP
UPDATE sales SET custno =
customer_id
WHERE serial_num =
serial_num_seq.NEXTVAL;
END LOOP; 6
WHILE LOOP
• The WHILE-LOOP statement associates a condition with a
sequence of statements. Before each iteration of the loop, the
condition is evaluated. If TRUE, loop continues. If FALSE the
loop ends and control passes to the next statement
WHILE LOOP
WHILE condition LOOP
sequence_of_statements;
END LOOP;
WHILE salary < 4000 LOOP
SELECT sal, ename
INTO salary, last_name
FROM emp ;
END LOOP;
7
Cursor
• A cursor is a pointer that points to a result of a
query.
• Oracle uses work areas (cursors) to execute SQL
statements and store processing information.
• There are two kinds of cursors:
– Implicit Cursors: PL/SQL implicitly declares a cursor for
all SQL data manipulation statements, including queries
that return only one row.
– Explicit Cursors: For queries that return more than one
row, you can explicitly declare a cursor t
8
Cursor
• DECLARE
• CURSOR c1 IS
• SELECT empno, ename, job
• FROM emp WHERE deptno = 20;
• emp_rec c1%ROWTYPE;
• The set of rows returned by a multi-row query is called the
result set. An explicit cursor "points" to the current row in the
result set. This allows your program to process the rows one
at a time.
9
Cursor
• Manipulating Cursors
• You use the OPEN, FETCH, and CLOSE statements
to control a cursor. The OPEN statement executes
the query associated with the cursor, identifies
the result set, and positions the cursor before
the first row. The FETCH statement retrieves the
current row and advances the cursor to the next
row. When the last row has been processed, the
CLOSE statement disables the cursor.
10
Cursor example
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
/
11
Questions?
12
Andrew Ng