ITEC 212 Manual Part 1 Programs
ITEC 212 Manual Part 1 Programs
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
Output
Statement processed.
THE PREV VALUES OF A AND B WERE5
5
4
THE VALUES OF A AND B ARE
4
5
Exercise 4
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
select * from lecturer;
Output:
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
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
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
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
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