Skip to content

Commit eea045d

Browse files
authoredJan 18, 2023
Day-41_ORACLE_PLSQL_CONTROL_STATEMENTS.txt
Day-41_ORACLE_PLSQL_CONTROL_STATEMENTS.txt
1 parent f67e1b7 commit eea045d

File tree

1 file changed

+280
-0
lines changed

1 file changed

+280
-0
lines changed
 
+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
"Welcome To Ashok IT"
2+
"Oracle Database"
3+
Topic : Introduction To PL/SQL-Control Statements
4+
Date : 18/01/2023
5+
(Session - 41)
6+
_____________________________________________________________________________________________________________________________
7+
Important Information
8+
*********************
9+
>> Oracle Class Notes ::: https://github.com/ashokitschool/ORACLE_CLASS_NOTES
10+
11+
>> Class Recording ::: Will be available through Ashok IT Portal
12+
13+
>> Class Related Updates "Join In WhatsApp Group" check with Admin Team.
14+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15+
Yesterday session
16+
=================
17+
* We executed some PL/SQL blocks using oracle Command Line Interface.
18+
19+
* If PL/SQL block wanted to retrieve the particular row from database table always recommended to use %rowtype type.
20+
21+
Example
22+
=======
23+
declare
24+
customerId number := &customerId;
25+
customer_record ashokit_customers%rowtype
26+
begin
27+
select * into customer_record from ashokit_customers where customer_id=customerId;
28+
dbms_output.put_line(customer_record.customer_name || customer_record.bill_amount);
29+
end;
30+
/
31+
32+
Today Session
33+
=============
34+
%type
35+
=====
36+
* By using %type object we can declare plsql variable according to the column datatype in database table.
37+
38+
Consider we have emp table which contains the below columns
39+
40+
empno >>>>>>>>>> number
41+
42+
ename >>>>>>>>>> varchar2
43+
44+
job >>>>>>>>>> varchar2
45+
46+
Plsql Block
47+
===========
48+
declare
49+
50+
employeeno emp.empno %type;
51+
employeename emp.ename %type;
52+
employeejob emp.job %type;
53+
54+
begin
55+
-- reading the input from enduser
56+
employeeno := &employeeno;
57+
select ename,job into employeename,employeejob from emp where empno=employeeno;
58+
dbms_output.put_line('Employee Name::::' || employeename);
59+
dbms_output.put_line('Employee Job ::::' || employeejob);
60+
end;
61+
/
62+
63+
Plsql Control Statements
64+
========================
65+
* Inorder to write some conditions in program definetly need to take support control statements.
66+
67+
* In plsql we have two types of control statements
68+
69+
1) conditional control statements
70+
71+
- simple if
72+
73+
- if-else
74+
75+
- nested if
76+
77+
- case
78+
79+
2) looping control statements
80+
81+
- basic
82+
83+
- while
84+
85+
- for
86+
87+
88+
1) IF-THEN Statement >>>>>>>>>>>>> Simple If Statement
89+
=========================================================
90+
* The IF-THEN statement is mainly used to execute a particular section of codes only when the condition is satisfied.
91+
92+
Syntax
93+
======
94+
IF <condition: returns Boolean> THEN
95+
-- executed only if the condition returns TRUE
96+
<action_block>
97+
END if;
98+
99+
Program
100+
=======
101+
DECLARE
102+
a NUMBER := &a;
103+
BEGIN
104+
dbms_output.put_line(‘Program started.' );
105+
IF( a > 100 ) THEN
106+
dbms_output.put_line('a is greater than 100');
107+
END IF;
108+
dbms_output.put_line(‘Program completed.');
109+
END;
110+
/
111+
112+
113+
2) IF-THEN-ELSE Statement >>>>>>>>>>>>>>>if-else statement
114+
============================================================
115+
* The IF-THEN-ELSE statement is mainly used to select between two alternatives based on the condition.
116+
117+
Syntax
118+
======
119+
IF <condition: returns Boolean> THEN
120+
-executed only if the condition returns TRUE
121+
<action_blockl>
122+
ELSE
123+
-execute if the condition failed (returns FALSE)
124+
<action_block2>
125+
END if;
126+
127+
Program
128+
=======
129+
DECLARE
130+
a NUMBER:= &provideValue;
131+
BEGIN
132+
dbms_output.put_line (‘Program started');
133+
IF(mod(a,2)=0) THEN
134+
dbms_output.put_line('Given Number is Even' );
135+
ELSE
136+
dbms_output.put_line('Given Number is Odd');
137+
END IF;
138+
dbms_output.put_line (‘Program completed.’);
139+
END;
140+
/
141+
142+
3) IF-THEN-ELSIF Statement
143+
==========================
144+
* 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.
145+
146+
* The first conditions that return <TRUE> will be executed, and the remaining conditions will be skipped.
147+
148+
* The IF-THEN-ELSIF statement may contain ‘ELSE’ block in it. This ‘ELSE’ block will be executed if none of the conditions is satisfied.
149+
150+
Syntax
151+
=====
152+
IF <conditionl: returns Boolean> THEN
153+
-- executed only if the condition returns TRUE
154+
<action_blockl>
155+
156+
ELSIF <condition2 returns Boolean>
157+
<action_block2>
158+
159+
ELSIF <condition3:returns Boolean>
160+
<action_block3>
161+
162+
ELSE — optional
163+
<action_block_else>
164+
165+
END if;
166+
167+
168+
Program
169+
======
170+
DECLARE
171+
mark NUMBER :=&studentMarks;
172+
BEGIN
173+
dbms_output.put_line(‘Program started.’ );
174+
IF( mark >= 70) THEN
175+
dbms_output.put_line(‘Grade A’);
176+
ELSIF(mark >= 40 AND mark < 70) THEN
177+
dbms_output.put_line(‘Grade B');
178+
ELSIF(mark >=35 AND mark < 40) THEN
179+
dbms_output.put_line(‘Grade C’);
180+
END IF;
181+
dbms_output.put_line(‘Program completed.’);
182+
END;
183+
/
184+
185+
4) case-when statement
186+
=======================
187+
* If we try to describe the case statement in one line then, then we can say means "one out of many".
188+
189+
* It is a decision making statement that selects only one option out of the multiple available options.
190+
191+
Syntax
192+
=====
193+
CASE selector
194+
when value1 then Statement1;
195+
when value2 then Statement2;
196+
...
197+
...
198+
else statement;
199+
end CASE;
200+
201+
Example
202+
=======
203+
DECLARE
204+
a number;
205+
b number;
206+
BEGIN
207+
a := &a;
208+
b := mod(a,2);
209+
CASE b
210+
when 0 then dbms_output.put_line('Even Number');
211+
when 1 then dbms_output.put_line('Odd Number');
212+
else dbms_output.put_line('User has not given any input value to check');
213+
END CASE;
214+
END;
215+
216+
looping control statement
217+
============================
218+
219+
1) Basic loop
220+
=============
221+
LOOP
222+
sequence of statements
223+
END LOOP;
224+
225+
Example
226+
=======
227+
DECLARE
228+
i number;
229+
BEGIN
230+
i := 1;
231+
LOOP
232+
if i>10 then
233+
exit;
234+
end if;
235+
dbms_output.put_line('i = ' || i);
236+
i := i+1;
237+
END LOOP;
238+
END;
239+
240+
While loop
241+
===========
242+
WHILE <test_condition> LOOP
243+
<action>
244+
END LOOP;
245+
246+
Example
247+
=======
248+
set serveroutput on;
249+
250+
DECLARE
251+
num number:=1;
252+
BEGIN
253+
while(num <= 10) LOOP
254+
dbms_output.put_line('num'|| num);
255+
num := num+2;
256+
END LOOP;
257+
END;
258+
259+
260+
for loop
261+
=======
262+
FOR counter_variable IN start_value..end_value LOOP
263+
264+
statement to be executed
265+
266+
END LOOP;
267+
268+
269+
Example
270+
=======
271+
set serveroutput on;
272+
273+
DECLARE
274+
i number(2);
275+
BEGIN
276+
FOR i IN 1..10 LOOP
277+
dbms_output.put_line(i);
278+
END LOOP;
279+
END;
280+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

0 commit comments

Comments
 (0)
Please sign in to comment.