-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPL-SQL Branch and Loops.sql
171 lines (154 loc) · 3.57 KB
/
PL-SQL Branch and Loops.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
-- PL-SQL Branch and Loops Examples
SET SERVEROUTPUT ON;
-- Branching
DECLARE
myName VARCHAR2(15) := 'Name1';
yourName VARCHAR2(15) := 'Name2';
BEGIN
IF myName = yourName THEN
DBMS_OUTPUT.PUT_LINE('Our names are the same!');
END IF;
END;
/
DECLARE
myName VARCHAR2(15) := 'Jaewon';
yourName VARCHAR2(15) := 'Jaewon';
BEGIN
IF myName = yourName THEN
DBMS_OUTPUT.PUT_LINE('Our names are the same!');
END IF;
END;
/
DECLARE
sales NUMBER(8, 2) := 12100;
quota NUMBER(8, 2) := 10000;
bonus NUMBER(6, 2);
BEGIN
IF sales > (quota + 200) THEN
bonus := (sales - quota) / 4;
ELSE
bonus := 50;
END IF;
DBMS_OUTPUT.PUT_LINE('The final bouns is... ' || bonus);
END;
/
BEGIN
IF DBMS_RANDOM.value(0, 1) > 0.5 THEN
DBMS_OUTPUT.PUT_LINE('Head');
ELSE DBMS_OUTPUT.PUT_LINE('Tail');
END IF;
END;
/
DECLARE
grade CHAR(1);
BEGIN
grade := 'B';
IF grade = 'A' THEN
DBMS_OUTPUT.PUT_LINE('Excellent');
ELSIF grade = 'B' THEN
DBMS_OUTPUT.PUT_LINE('Very Good');
ELSIF grade = 'C' THEN
DBMS_OUTPUT.PUT_LINE('Good');
ELSIF grade = 'D' THEN
DBMS_OUTPUT.PUT_LINE('Fair');
ELSIF grade = 'F' THEN
DBMS_OUTPUT.PUT_LINE('Poor');
ELSE DBMS_OUTPUT.PUT_LINE('No such grade');
END IF;
END;
/
DECLARE
grade CHAR(1);
BEGIN
grade := 'B';
CASE grade
WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');
WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very Good');
WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Good');
WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair');
WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor');
ELSE DBMS_OUTPUT.PUT_LINE('No such grade');
END CASE;
END;
/
-- Loops
DECLARE
hundreds_counter NUMBER(3);
BEGIN
hundreds_counter := 100;
LOOP DBMS_OUTPUT.PUT_LINE(hundreds_counter);
hundreds_counter := hundreds_counter + 100;
END LOOP;
END;
/
DECLARE
hundreds_counter NUMBER(3);
BEGIN
hundreds_counter := 100;
LOOP DBMS_OUTPUT.PUT_LINE(hundreds_counter);
hundreds_counter := hundreds_counter + 100;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('That is as high as you can go.');
END;
/
DECLARE
hundreds_counter NUMBER(3);
BEGIN
hundreds_counter := 100;
WHILE hundreds_counter <= 800
LOOP DBMS_OUTPUT.PUT_LINE(hundreds_counter);
hundreds_counter := hundreds_counter + 100;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('That is as high as you can go.');
END;
/
DECLARE
i NUMBER := 1;
BEGIN
LOOP i := i + 1;
EXIT WHEN i >= 10;
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/
DECLARE
fin CHAR(50) := 'That''s all numbers between 50 and 55';
counter NUMBER(3);
BEGIN
FOR counter IN 50 .. 55
LOOP DBMS_OUTPUT.PUT_LINE('Counter = ' || counter);
END LOOP;
DBMS_OUTPUT.PUT_LINE(fin);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('This would have been an error.');
END;
/
DECLARE
fin CHAR(50) := 'That''s all numbers between 55 and 50';
counter NUMBER(3);
BEGIN
FOR counter IN REVERSE 50 .. 55
LOOP DBMS_OUTPUT.PUT_LINE('Counter = ' || counter);
END LOOP;
DBMS_OUTPUT.PUT_LINE(fin);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('This would have been an error.');
END;
/
DECLARE
counter NUMBER(3);
BEGIN
FOR counter IN -5 .. 5
LOOP DBMS_OUTPUT.PUT_LINE('37 divided by ' || counter || ' = ' || (37 / counter));
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('This would have been an error.');
END;
/