Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

///////////////////////////////////////////////////////////////////////////////////

//
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

PL/SQL procedure successfully completed.


///////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
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

Select/Insert/update/delete work on different tables for different


purpose/transaction

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;

DBMS_OUTPUT.PUT_LINE('My location is '|| v_location_id ||' '||


v_street_address);
END;
/

=============================================
-- using variable binding to display the output
-- Bind variables are variables you create in SQL*Plus and then reference in PL/SQL

VARIABLE return_msg varchar2(100);

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;

msg := to_char(v_location_id) ||' '||v_street_address;

select msg into :return_msg


from dual;
END;
/
SET AUTOPRINT ON
print :return_msg
=============================================

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

dbms_output.put_line( ' salary is '||v_sal);

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('invalid number ');
END;
/

====================================

--non-defined error, error do not happened frequently


SET SERVEROUTPUT ON
DECLARE
INSERT_EXCEP EXCEPTION;
PRAGMA EXCEPTION_INIT
(INSERT_EXCEP, -01400);
BEGIN
INSERT INTO DEPARTMENTS (department_id, department_name)
VALUES (280, null);

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 ========================


/*
In-class exercise
Write a command to declare a date variable named current_date, and assign to it the
current system date. Depending on the day of the month, your program should display
the following output:
Day Output
1-10 It is Day <day number> of<month name>.
It is early in the month.
11-20 It is Day <day number> of<month name>.
It is the middle of the month.
21-31 It is Day <day number> of<month name>.
It is nearly the end of the month.
For example, if it is currently November 30th, your program should display "It is
Day 30 of November. It is nearly the end of the month." Declare and use additional
variables as needed.
*/
CLEAR SCR
SET SERVEROUTPUT ON
DECLARE
v_day VARCHAR2(20):= TO_CHAR(SYSDATE, 'dd');
v_month VARCHAR2(20) := TRIM(TO_CHAR(SYSDATE, 'Month'));
BEGIN
IF v_day between 1 and 10 THEN
DBMS_OUTPUT.PUT_LINE('It is early in the month.');
DBMS_OUTPUT.PUT_LINE('It is Day '|| v_day || ' of '|| v_month || chr(10)|| 'It
is early in the month.');
ELSIF v_day between 11 and 19 THEN
DBMS_OUTPUT.PUT_LINE('It is Day '|| v_day|| ' of '|| v_month ||chr(10)||'It is
the middle of the month.');
ELSIF v_day between 21 and 31 THEN
DBMS_OUTPUT.PUT_LINE('It is Day '|| v_day|| ' of '|| v_month || chr(10)||'It
is nearly the end of the month.');

END IF;
END;
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////

You might also like