Professional Documents
Culture Documents
Lab 5
Lab 5
//
DO WHILE LOOP
-----------------------------------------------------------------------------------
-
DECLARE
v_Counter NUMBER;
BEGIN
V_Counter:=1;
LOOP
V_Counter := V_Counter +1;
EXIT WHEN V_Counter >16;
DBMS_OUTPUT.PUT_LINE('Hello');
END LOOP;
END;
OUTPUT:
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
/////////////////////////////////////////////////////////////////////////////////
WHILE LOOP
---------------------------------------------------
DECLARE
a number(2) := 10;
BEGIN
WHILE a<20 LOOP
DBMS_OUTPUT.PUT_LINE('Value of a: '||a);
a :=a+1;
END LOOP;
END;
OUTPUT:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
FOR LOOP
----------------------------------------
DECLARE
a number(2);
BEGIN
FOR a in 10..20 LOOP
DBMS_OUTPUT.PUT_LINE('Value of a: ' ||a);
END LOOP;
END;
OUTPUT:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19
Value of a: 20
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
IMPLICIT CURSOR
------------------------------------------------
DECLARE
total_rows number(2);
BEGIN
UPDATE employees
SET salary = salary + 500 WHERE department_id='20';
IF sql%notfound THEN
DBMS_OUTPUT.PUT_LINE('No employees selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
DBMS_OUTPUT.PUT_LINE(total_rows || ' employees selected');
END IF;
END;
OUTPUT:
2 employees selected
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
EXPLICIT CURSOR (NOT WORKING)
----------------------------------------------------------
DECLARE
e_id employees.id%type;
e_name employees.last_name%type;
e_did employees.department_id%type;
CURSOR e_employees IS
select employee_id,last_name,department_id from employees;
BEGIN
Open e_employees;
LOOP
Fetch e_employees INTO e_id,e_name,e_addr;
EXIT WHEN e_customers%notfound;
DBMS_OUTPUT.PUT_LINE(e_id||' '||e_name||' '||e_addr);
END LOOP;
Close e_employees;
END;
OUTPUT:
e_id employees.id%type;
*
ERROR at line 2:
ORA-06550: line 2, column 16:
PLS-00302: component 'ID' must be declared
ORA-06550: line 0, column 0:
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
EXCEPTION
-------------------------------
1.
DECLARE
c_id customers.id%type := 8;
c_name customerS.Name%type;
c_addr customers.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
OUTPUT:
2.
DECLARE
c_id customers.id%type := &cc_id;
c_name customerS.Name%type;
c_addr customers.address%type;
-- user defined exception
ex_invalid_id EXCEPTION;
BEGIN
IF c_id <= 0 THEN
RAISE ex_invalid_id;
ELSE
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
END IF;
EXCEPTION
WHEN ex_invalid_id THEN
dbms_output.put_line('ID must be greater than zero!');
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
OUTPUT:
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////
UCCD2303 202301 Practical 5 PLSQL constructs extra examples
Prepared by Chan LK
SET SERVEROUTPUT ON
DECLARE
v_location_id locations.location_id%type;
v_street_address locations.street_address%type;
BEGIN
SELECT location_id, street_address
INTO v_location_id, v_street_address
FROM locations
WHERE location_id=3200;
=============================================
-- using variable binding to display the output
-- Bind variables are variables you create in SQL*Plus and then reference in PL/SQL
DECLARE
v_location_id locations.location_id%type;
v_street_address locations.street_address%type;
msg varchar2(100);
BEGIN
select location_id, street_address
into v_location_id, v_street_address
from locations
where location_id=1000;
SET SERVEROUTPUT ON
DECLARE
CURSOR e1 IS
select location_id, street_address
from locations;
vlocation_id locations.location_id%type;
vstreet_address locations.street_address%type;
BEGIN
OPEN e1;
LOOP
FETCH e1 into vlocation_id , vstreet_address;
EXIT when e1%notfound;
DBMS_OUTPUT.PUT_LINE( vlocation_id ||' '||vstreet_address );
END LOOP;
END;
/
===================================================================================
==-- exception errors examples (predefined, non-defined, user-defined)
-- predefined exception
SET SERVEROUTPUT ON
DECLARE
v_sal number (7, 2);
BEGIN
select salary into v_sal
from employees
where employee_id = 299;--205 valid
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('invalid number ');
END;
/
====================================
EXCEPTION
WHEN INSERT_EXCEP THEN
DBMS_OUTPUT.PUT_LINE('An exception is caught: insert operation is failed');
DBMS_OUTPUT.PUT_LINE(sqlerrm);
END;
/
====================================
DECLARE
invalid_department_exp EXCEPTION;
name varchar2(20):='abc';
deptno number :=99999;
BEGIN
update departments
set department_name = name
where department_id = deptno;
IF SQL%NOTFOUND THEN
raise invalid_department_exp;
END IF;
COMMIT;
EXCEPTION
WHEN invalid_department_exp THEN
DBMS_OUTPUT.PUT_LINE('no such department id.');
END;
/
END IF;
END;
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////