-
Notifications
You must be signed in to change notification settings - Fork 58
/
Day-41_ORACLE_PLSQL_CONTROL_STATEMENTS.txt
280 lines (222 loc) · 6.75 KB
/
Day-41_ORACLE_PLSQL_CONTROL_STATEMENTS.txt
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"Welcome To Ashok IT"
"Oracle Database"
Topic : Introduction To PL/SQL-Control Statements
Date : 18/01/2023
(Session - 41)
_____________________________________________________________________________________________________________________________
Important Information
*********************
>> Oracle Class Notes ::: https://github.com/ashokitschool/ORACLE_CLASS_NOTES
>> Class Recording ::: Will be available through Ashok IT Portal
>> Class Related Updates "Join In WhatsApp Group" check with Admin Team.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Yesterday session
=================
* We executed some PL/SQL blocks using oracle Command Line Interface.
* If PL/SQL block wanted to retrieve the particular row from database table always recommended to use %rowtype type.
Example
=======
declare
customerId number := &customerId;
customer_record ashokit_customers%rowtype
begin
select * into customer_record from ashokit_customers where customer_id=customerId;
dbms_output.put_line(customer_record.customer_name || customer_record.bill_amount);
end;
/
Today Session
=============
%type
=====
* By using %type object we can declare plsql variable according to the column datatype in database table.
Consider we have emp table which contains the below columns
empno >>>>>>>>>> number
ename >>>>>>>>>> varchar2
job >>>>>>>>>> varchar2
Plsql Block
===========
declare
employeeno emp.empno %type;
employeename emp.ename %type;
employeejob emp.job %type;
begin
-- reading the input from enduser
employeeno := &employeeno;
select ename,job into employeename,employeejob from emp where empno=employeeno;
dbms_output.put_line('Employee Name::::' || employeename);
dbms_output.put_line('Employee Job ::::' || employeejob);
end;
/
Plsql Control Statements
========================
* Inorder to write some conditions in program definetly need to take support control statements.
* In plsql we have two types of control statements
1) conditional control statements
- simple if
- if-else
- nested if
- case
2) looping control statements
- basic
- while
- for
1) IF-THEN Statement >>>>>>>>>>>>> Simple If Statement
=========================================================
* The IF-THEN statement is mainly used to execute a particular section of codes only when the condition is satisfied.
Syntax
======
IF <condition: returns Boolean> THEN
-- executed only if the condition returns TRUE
<action_block>
END if;
Program
=======
DECLARE
a NUMBER := &a;
BEGIN
dbms_output.put_line(‘Program started.' );
IF( a > 100 ) THEN
dbms_output.put_line('a is greater than 100');
END IF;
dbms_output.put_line(‘Program completed.');
END;
/
2) IF-THEN-ELSE Statement >>>>>>>>>>>>>>>if-else statement
============================================================
* The IF-THEN-ELSE statement is mainly used to select between two alternatives based on the condition.
Syntax
======
IF <condition: returns Boolean> THEN
-executed only if the condition returns TRUE
<action_blockl>
ELSE
-execute if the condition failed (returns FALSE)
<action_block2>
END if;
Program
=======
DECLARE
a NUMBER:= &provideValue;
BEGIN
dbms_output.put_line (‘Program started');
IF(mod(a,2)=0) THEN
dbms_output.put_line('Given Number is Even' );
ELSE
dbms_output.put_line('Given Number is Odd');
END IF;
dbms_output.put_line (‘Program completed.’);
END;
/
3) IF-THEN-ELSIF Statement
==========================
* The IF-THEN-ELSIF statement is mainly used where one alternative should be chosen from a set of alternatives, where each alternative has its own conditions to be satisfied.
* The first conditions that return <TRUE> will be executed, and the remaining conditions will be skipped.
* The IF-THEN-ELSIF statement may contain ‘ELSE’ block in it. This ‘ELSE’ block will be executed if none of the conditions is satisfied.
Syntax
=====
IF <conditionl: returns Boolean> THEN
-- executed only if the condition returns TRUE
<action_blockl>
ELSIF <condition2 returns Boolean>
<action_block2>
ELSIF <condition3:returns Boolean>
<action_block3>
ELSE — optional
<action_block_else>
END if;
Program
======
DECLARE
mark NUMBER :=&studentMarks;
BEGIN
dbms_output.put_line(‘Program started.’ );
IF( mark >= 70) THEN
dbms_output.put_line(‘Grade A’);
ELSIF(mark >= 40 AND mark < 70) THEN
dbms_output.put_line(‘Grade B');
ELSIF(mark >=35 AND mark < 40) THEN
dbms_output.put_line(‘Grade C’);
END IF;
dbms_output.put_line(‘Program completed.’);
END;
/
4) case-when statement
=======================
* If we try to describe the case statement in one line then, then we can say means "one out of many".
* It is a decision making statement that selects only one option out of the multiple available options.
Syntax
=====
CASE selector
when value1 then Statement1;
when value2 then Statement2;
...
...
else statement;
end CASE;
Example
=======
DECLARE
a number;
b number;
BEGIN
a := &a;
b := mod(a,2);
CASE b
when 0 then dbms_output.put_line('Even Number');
when 1 then dbms_output.put_line('Odd Number');
else dbms_output.put_line('User has not given any input value to check');
END CASE;
END;
looping control statement
============================
1) Basic loop
=============
LOOP
sequence of statements
END LOOP;
Example
=======
DECLARE
i number;
BEGIN
i := 1;
LOOP
if i>10 then
exit;
end if;
dbms_output.put_line('i = ' || i);
i := i+1;
END LOOP;
END;
While loop
===========
WHILE <test_condition> LOOP
<action>
END LOOP;
Example
=======
set serveroutput on;
DECLARE
num number:=1;
BEGIN
while(num <= 10) LOOP
dbms_output.put_line('num'|| num);
num := num+2;
END LOOP;
END;
for loop
=======
FOR counter_variable IN start_value..end_value LOOP
statement to be executed
END LOOP;
Example
=======
set serveroutput on;
DECLARE
i number(2);
BEGIN
FOR i IN 1..10 LOOP
dbms_output.put_line(i);
END LOOP;
END;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++