0% found this document useful (0 votes)
24 views

ITEC 212 Manual Part 1 Programs

The document contains 13 exercises demonstrating the use of PL/SQL programming constructs like loops, conditional statements, exceptions, cursors and stored procedures. The exercises create and manipulate tables, insert and select data, define variables, use loops and conditional logic, handle exceptions and output results.

Uploaded by

Yasir Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

ITEC 212 Manual Part 1 Programs

The document contains 13 exercises demonstrating the use of PL/SQL programming constructs like loops, conditional statements, exceptions, cursors and stored procedures. The exercises create and manipulate tables, insert and select data, define variables, use loops and conditional logic, handle exceptions and output results.

Uploaded by

Yasir Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Exercise 1

CREATE TABLE items_tab (item_code varchar2(6) PRIMARY


KEY,item_descr varchar2(20) NOT NULL)

Output : Table created.

DECLARE
v_item_code VARCHAR2(6);
v_item_descr VARCHAR2(20);
BEGIN
v_item_code := 'ITM101';
v_item_descr := 'Spare parts';
INSERT INTO items_tab VALUES (v_item_code, v_item_descr);
EXCEPTION WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
/
select * from items_tab;

Statement processed.
Result Set 1
ITEM_CODE ITEM_DESCR
ITM101 Spare parts

Exercise 2

declare
myNumber number := 1;
begin
myNumber := 1 + 1;
dbms_output.put_line( '1 + 1 = ' || to_char( myNumber ) || '!' );
exception
when others then
dbms_output.put_line( 'We encountered an exception!' );
end;

Output

Statement processed.
1 + 1 = 2!
Exercise 3
declare
a number(10):=5;
b number(10):=4;
begin

dbms_output.put_line('THE PREV VALUES OF A AND B WERE'||


to_char(a));
dbms_output.put_line(a);
dbms_output.put_line(b);
a:=a+b;
b:=a-b;
a:=a-b;
dbms_output.put_line('THE VALUES OF A AND B ARE');
dbms_output.put_line(a);
dbms_output.put_line(b);
end;

Output

Statement processed.
THE PREV VALUES OF A AND B WERE5
5
4
THE VALUES OF A AND B ARE
4
5

Exercise 4

CREATE TABLE lecturer (id NUMBER(5) PRIMARY KEY,first_name


VARCHAR2(20),last_name VARCHAR2(20),major
VARCHAR2(30),current_credits NUMBER(3));
Output
Table Created

INSERT INTO lecturer (id, first_name, last_name,


major,current_credits)
VALUES (10003, 'Jone', 'Bliss','Computer Science', 8);
INSERT INTO lecturer (id, first_name, last_name,
major,current_credits)
VALUES (10004, 'Man', 'Kyte','Economics', 8);
INSERT INTO lecturer (id, first_name, last_name,
major,current_credits)
VALUES (10005, 'Pat', 'Poll','History', 4);
INSERT INTO lecturer (id, first_name, last_name,
major,current_credits)
VALUES (10006, 'Tim', 'Viper','History', 4);

1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
select * from lecturer;

Output:

FIRST_NAME LAST_NAME MAJOR CURRENT_CREDITS


ID
10003 Jone Bliss Computer Science 8
10004 Man Kyte Economics 8
10005 Pat Poll History 4
10006 Tim Viper History 4

CREATE TABLE mylogtable (code NUMBER, message VARCHAR2(200),


info VARCHAR2(100));

DECLARE
myLecturerID NUMBER(5) := 10000;
firstName VARCHAR2(20);
BEGIN
SELECT first_name
INTO firstName
FROM lecturer
WHERE id = myLecturerID;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO myLogTable (info) VALUES ('Student 10,000 does not
exist!');
END;
/
Select * from mylogTable

MESSAGE INFO
CODE
- - Student 10,000 does not exist!

Exercise 5

DECLARE
v_HoursWorked Number := 50 ;
v_OverTime Number := 0 ;
BEGIN
IF v_HoursWorked > 40 THEN
v_OverTime := v_HoursWorked - 40;
DBMS_OUTPUT.PUT_LINE('Hours overtime worked = ' || v_OverTime);
END IF;
END;

Output:

Statement processed.
Hours overtime worked = 10
Exercise 6

CREATE TABLE place (room_id NUMBER(5) PRIMARY KEY,building


VARCHAR2(15),room_number NUMBER(4),number_seats
NUMBER(4),description VARCHAR2(50));

INSERT INTO place (room_id, building, room_number, number_seats,


description)
VALUES (20001, 'Building 7', 201, 1000, 'Large Lecture Hall');
INSERT INTO place (room_id, building, room_number, number_seats,
description)
VALUES (20002, 'Building 6', 101, 500, 'Small Lecture Hall');
INSERT INTO place (room_id, building, room_number, number_seats,
description)
VALUES (20003, 'Building 6', 150, 50, 'Discussion Room A');
INSERT INTO place (room_id, building, room_number, number_seats,
description)
VALUES (20004, 'Building 6', 160, 50, 'Discussion Room B');
INSERT INTO place (room_id, building, room_number,
number_seats,description)
VALUES (20005, 'Building 6', 170, 50, 'Discussion Room C');
INSERT INTO place (room_id, building, room_number, number_seats,
description)
VALUES (20006, 'Music Building', 100, 10, 'Music Practice
Room');
INSERT INTO place (room_id, building, room_number, number_seats,
description)
VALUES (20007, 'Music Building', 200, 1000, 'Concert Room');
INSERT INTO place (room_id, building, room_number, number_seats,
description)
VALUES (20008, 'Building 7', 300, 75, 'Discussion Room D');
INSERT INTO place (room_id, building, room_number,
number_seats,description)
VALUES (20009, 'Building 7', 310, 50, 'Discussion Room E');

SELECT * from place;

DECLARE
v_NumberSeats place.number_seats%TYPE;
v_Comment VARCHAR2(35);
BEGIN
SELECT number_seats INTO v_NumberSeats FROM place WHERE room_id =
20008;
IF v_NumberSeats < 50 THEN
v_Comment := 'Fairly small';
ELSIF v_NumberSeats < 100 THEN
v_Comment := 'A little bigger';
ELSE
v_Comment := 'Lots of room';
END IF;
END;
/
select * from place;

Exercise 7

DECLARE
variable_Score Number := 85;
variable_LetterGrade Char(1);
BEGIN
IF variable_Score >= 90 THEN
variable_LetterGrade := 'A';
ELSIF variable_Score >= 80 THEN
variable_LetterGrade := 'B';
ELSIF variable_Score >= 70 THEN
variable_LetterGrade := 'C';
ELSIF variable_Score >= 60 THEN
variable_LetterGrade := 'D';
ELSE
variable_LetterGrade := 'E';
END IF;
DBMS_OUTPUT.PUT_LINE('Your Letter Grade is: ' ||
variable_LetterGrade);
END;

Output:
Statement processed.
Your Letter Grade is: B
Exercise 8

CREATE TABLE MyTable(num_col number(5),char_col varchar2(60))

Output : Table created.

DECLARE
v_Counter BINARY_INTEGER := 1;
BEGIN
LOOP
INSERT INTO MyTable VALUES (v_Counter, 'Loopindex');
v_Counter := v_Counter + 1;
IF v_Counter > 50 THEN
EXIT;
END IF;
END LOOP;
END;

Output

Statement processed.
NUM_COL CHAR_COL
1 Loopindex
2 Loopindex
3 Loopindex
4 Loopindex
5 Loopindex
6 Loopindex
7 Loopindex
8 Loopindex
9 Loopindex
10 Loopindex
11 Loopindex
12 Loopindex
13 Loopindex
14 Loopindex
15 Loopindex
16 Loopindex
17 Loopindex
18 Loopindex
19 Loopindex
20 Loopindex
21 Loopindex
22 Loopindex
23 Loopindex
24 Loopindex
25 Loopindex
26 Loopindex
27 Loopindex
28 Loopindex
29 Loopindex
30 Loopindex
31 Loopindex
32 Loopindex
33 Loopindex
34 Loopindex
35 Loopindex
36 Loopindex
37 Loopindex
38 Loopindex
39 Loopindex
40 Loopindex
41 Loopindex
42 Loopindex
43 Loopindex
44 Loopindex
45 Loopindex
46 Loopindex
47 Loopindex
48 Loopindex
49 Loopindex
50 Loopindex

Exercise 9

DECLARE
v_Radius NUMBER := 2;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('The AREA of the circle is ' ||
3.14*v_RADIUS*v_RADIUS);
v_Radius := v_Radius + 2;
EXIT WHEN v_Radius > 10;
END LOOP;
END;

Output

Statement processed.
The AREA of the circle is 12.56
The AREA of the circle is 50.24
The AREA of the circle is 113.04
The AREA of the circle is 200.96
The AREA of the circle is 314

Exercise 10

DECLARE
v_count PLS_INTEGER := 1;
BEGIN
WHILE v_count <= 10
LOOP
DBMS_OUTPUT.PUT_LINE('While loop iteration: '||v_count);
v_count := v_count + 1;
END LOOP;
END;

Output:
Statement processed.
While loop iteration: 1
While loop iteration: 2
While loop iteration: 3
While loop iteration: 4
While loop iteration: 5
While loop iteration: 6
While loop iteration: 7
While loop iteration: 8
While loop iteration: 9
While loop iteration: 10

Exercise 11
BEGIN
FOR i IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i);
END LOOP;
END;

Output:
Statement processed.
Loop counter is 1
Loop counter is 2
Loop counter is 3
Loop counter is 4
Loop counter is 5

Exercise 12
BEGIN
FOR i IN 1..2
LOOP
FOR j IN 1..4
LOOP
DBMS_OUTPUT.PUT_LINE('Outer Loop counter is '||
i || ' Inner Loop counter is ' || j);
END LOOP;
END LOOP;
END;

Output :

Statement processed.
Outer Loop counter is 1 Inner Loop counter is 1
Outer Loop counter is 1 Inner Loop counter is 2
Outer Loop counter is 1 Inner Loop counter is 3
Outer Loop counter is 1 Inner Loop counter is 4
Outer Loop counter is 2 Inner Loop counter is 1
Outer Loop counter is 2 Inner Loop counter is 2
Outer Loop counter is 2 Inner Loop counter is 3
Outer Loop counter is 2 Inner Loop counter is 4

Exercise 13

DECLARE
loop_start Integer := 1;
BEGIN
FOR i IN REVERSE loop_start..5 LOOP
DBMS_OUTPUT.PUT_LINE('Loop counter is ' || i);
END LOOP;
END;
Output:

Statement processed.
Loop counter is 5
Loop counter is 4
Loop counter is 3
Loop counter is 2
Loop counter is 1

Exercise 14

CREATE TABLE MyTable (


num_col NUMBER,
char_col VARCHAR2(60))

BEGIN
FOR v_Count IN 1..10 LOOP
INSERT INTO MyTable (num_col, char_col)
VALUES (v_Count, 'Hello World!');
END LOOP;
END;
/
select * from MyTable;

Output:

NUM_COL CHAR_COL
1 Hello World!
2 Hello World!
3 Hello World!
4 Hello World!
5 Hello World!
6 Hello World!
7 Hello World!
8 Hello World!
9 Hello World!
10 Hello World!
Exercise 15

CREATE TABLE MyTable1(num_col number(5),char_col varchar2(60));

DECLARE
v_LowValue NUMBER := 10;
v_HighValue NUMBER := 18;
BEGIN
FOR v_Counter IN REVERSE v_LowValue .. v_HighValue LOOP
INSERT INTO MyTable1 VALUES (v_Counter, 'Dynamically specified
loop ranges');
END LOOP;
END;
/
select * from MyTable1;

Output:
Table Created.
Statement processed.

NUM_COL CHAR_COL
18 Dynamically specified loop ranges
17 Dynamically specified loop ranges
16 Dynamically specified loop ranges
15 Dynamically specified loop ranges
14 Dynamically specified loop ranges
13 Dynamically specified loop ranges
12 Dynamically specified loop ranges
11 Dynamically specified loop ranges
10 Dynamically specified loop ranges

You might also like