PLSQL
PLSQL
SYNTAX
DECLARE
Variable,Constant,Records & Cursor declaration
BEGIN
SQL , Loop,Condetional Statement
EXCEPTION
Exception handling
END;
/ EXECUTE THE ABOVE PL/SQL BLOCK
Advantages of PL/SQL
DECLARE
var_salary number(6);
var_emp_id number(6) = 1116;
BEGIN
SELECT salary INTO var_salary FROM employee WHERE emp_id =
var_emp_id;
dbms_output.put_line(var_salary); dbms_output.put_line('The employee '
|| var_emp_id || ' has salary ' || var_salary);
END;
/
Scope of Variables
• Local variables
• Global variables
DECLARE
var_num1 number;
var_num2 number;
BEGIN
var_num1 := 100;
var_num2 := 200;
DECLARE
var_mult number;
BEGIN
var_mult := var_num1 * var_num2;
END;
END;
/
PL/SQL Constants
• General Syntax :
constant_name CONSTANT datatype := VALUE;
DECLARE
salary_increase CONSTANT number(3);
BEGIN
salary_increase := 100;
dbms_output.put_line(salary_increase);
END;
/
PL/SQL Records
• General Syntax :
LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
PL/SQL WHILE Loop
declare
v_test varchar2(8) := 'RUN';
n_numb number := 2;
begin
while v_test <> 'STOP' loop
if n_numb > 5
then v_test := 'STOP';
end if;
dbms_output.put_line (v_test||': '||n_numb);
n_numb := n_numb + 1;
end loop;
v_test := 'DOWN';
while n_numb > 1 AND v_test = 'DOWN' loop
dbms_output.put_line (v_test||': '||n_numb);
n_numb := n_numb - 1;
end loop;
while 7 = 4 loop
NULL; -- never get here
end loop;
end;
/
For Loop
declare
idx number := 100
begin
dbms_output.put_line (idx);
for idx in 2..5 loop
dbms_output.put_line (idx);
end loop;
dbms_output.put_line (idx);
end;
/
What are Cursors?
• A cursor is a temporary work area created in the system memory when
a SQL statement is executed. A cursor contains information on a select
statement and the rows of data accessed by it.
• There are two types of cursors in PL/SQL:
• Implicit cursors:
• These are created by default when DML statements like, INSERT,
UPDATE, and DELETE statements are executed. They are also created
when a SELECT statement that returns just one row is executed.
Parameters:SQL%FOUND , SQL%NOTFOUND , SQL%ROWCOUNT
• Explicit cursors:
• They must be created when you are executing a SELECT statement that
returns more than one row. Even though the cursor stores multiple
records, only one record can be processed at a time, which is called as
current row. When you fetch a row the current row position moves to
next row.
Implicit Cursors:
DECLARE
var_rows number(5);
BEGIN
UPDATE employee SET salary = salary + 1000;
IF SQL%NOTFOUND THEN
dbms_output.put_line('None of the salaries where updated');
ELSIF SQL%FOUND THEN
var_rows := SQL%ROWCOUNT;
dbms_output.put_line('Salaries for ' || var_rows || 'employees are
updated');
END IF;
END;
/
Explicit Cursors
• There are four steps in using an Explicit Cursor.
DECLARE the cursor in the declaration section.
OPEN the cursor in the Execution Section.
FETCH the data from cursor into PL/SQL variables or records in the
Execution Section.
CLOSE the cursor in the Execution Section before you end the PL/SQL
Block.
DECLARE
variables;
records;
create a cursor;
BEGIN
OPEN cursor;
FETCH cursor;
process the records;
CLOSE cursor;
END;
Explicit Cursors Example
DECLARE
emp_rec emp_tbl%rowtype;
CURSOR emp_cur IS
SELECT * FROM
WHERE salary > 1000;
BEGIN
OPEN emp_cur;
FETCH emp_cur INTO emp_rec;
dbms_output.put_line (emp_rec.first_name || ' ' ||
emp_rec.last_name);
CLOSE emp_cur;
END;
/