Control Structures in PL/SQL

17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
Already have an account? Log In

Transcript

Hi. In this lesson, we understand how to deal with control structures in PL SQL. In the previous lesson, we just got started, we wrote a small, anonymous block executed to understand what are the important ingredients of a PL SQL program. In this lesson, let's get into more details. To start with, just like any other language, pl SQL does support operators. The first of the operators is assignment operator, which is colon equal to.

It's not like many other languages where it is just a single equal to here it is called an equal to, you need to remember that and this expression is saying it is going to add amounted to total and then put the result back into it. Total. So, V total is the variable to which we assign the value and the value is coming from the right side expression. You just have to make sure the result of this expression is compatible with the data type of the variable that is simple assignment. Then the next thing is a dometic operator. These are automatic operators and we are talking about very standard a domestic operators.

Of course, you don't find operators like a plus plus minus minus, which are very common in languages like C Java etc. So, plus minus slash star are available everywhere. The missing operator is a modulus operator. We don't have any operator operator for getting the remainder of a division. But instead we have a function called mod, which is one of the other Matic functions in Oracle. So we still have to use the same.

And then we have an operator, the double star to raise bass to power. So if I say to double star, and if I give a it's going to give me 256. That's to raise it to the power of eight. So those are automatic operators in PL SQL. Then coming to functions, what functions can I use in PL SQL blocks, almost everything that you have in SQL except a few. Number one, you can't use decode, decode is only used in sacred number two, no growth functions can be used all these groups functions like min max, sum, average etc can be used only in SQL, not in PL SQL.

What I mean by that is, I can't do anything like this, this is not valid. But if you really want to get the total salary of all employees, then you can use it with the normal select and put the result into the variable using into this is very much valid. So though you can't use these functions directly in the procedural statement like this, you can use it in SQL statement, and the result can be copied into a variable of PL SQL block. So this is something you need to keep on your mind. Then, we have the same standard control statements everywhere. We have a statement and this is the syntax You just need to get used to the syntax.

If condition, then the statements, you can give any number of statements, then else if it's just l s, if there is no S II, okay? It's just LS if condition two, and then the statements. If this condition is satisfied, then these statements are executed, but it goes in the order. If first condition fails, then it goes to second. If second condition also fails, it might then go to third, fourth, and so on, because these three dots would indicate you can repeat this any number of times, but this is optional. That's what the square brackets would indicate.

So if you're not giving anything like L, F, then we directly go to ELLs. So that is Going to be a simple if else, if condition is true do this. If condition is false then do this. But if you have more conditions you can use this elsif okay though it is LS if we just pronounce it as elsif. Now, the rules says the control goes to else if condition is false or null null can really jeopardize a lot if you don't handle it carefully. So if condition happens to be null, it's considered to be false.

So, you have to be aware of it. When expression in condition contains null value the condition becomes null. So if I have something like a greater than B, and if a is now the whole result is null, that means it is false. You have to be aware of this point. So, if you feel there could be null values in the expressions being used in the condition take care of null values. So here is a relational operators list and we have the same operators we have been using even in SQL.

And we have the same greater than less than not a call to like between and and so on. So there is nothing new. It's the same old stuff. And then here is a small example. I want to find out the average salary of employees who are in the same job as employ 110. So find out the job ID of 110 and find out the average salary of all the employees where the job ID is whatever is the job ID of 110 let us say 110 is a sales manager, then we get average salary of sales managers and then I want to find out what is the salary of 110.

And then comes a simple if statement, if the salary of 110 is greater than average salary, then we are going to update a salary by 10% and we are updating the salary of employee 110 by 10% we are hiking we are incrementing it by 10% This is the expression. So, take the current salary multiplied with 1.1 which happens to be 10% if not, if a salary is not greater than the average salary, then we go to else portion and we do this and This is where we are incrementing salary by 20%. And whatever you do, eventually you need to give endif. And also it's a good practice, though I didn't do it here to commit the changes that are made. So this is how you execute some select command update commands in the context of a PL SQL block. So let us see how this works.

Here is the program. And this is the program where we take the average salary of employees who are doing the same job as 110. And if the salary of 110 is greater than average salary, we increment salary by 10%. So let's go run this and then go ahead and run and it says the procedures are successfully completed. That means it has either applied at 10% higher Got 20 depending on the case, you can as well put some extra messages here, just to know which way it is going. So select command, then if statement, and then we go either this way or that way.

That's about a simple if statement. Now, coming back, this is another very powerful case expression, where you have more than one condition, it's better use instead of multiple if statements like if elsif elsif. A case is always more readable. In programming languages like C we call it as switch, but here it is called as case expression. We also have another flavor called case statement, but to start with, case expression is saying it will take an expression They're called a selector. And it is going to compare this with the given values.

So the selector is being compared with first expression. If these two are same, it'll return this, it always returns a value. The value is going to be result one if selector is expression one, but if selector is not expression one, it will compare selector with expression two. If it is same, it returns result two and so on. But if none of the values matched, then it goes to else if given and it is going to return this value. So eventually case is going to return a value this whole thing will be returning a value.

Then we have another more flexible version of the same This time this is without selector, there is no selector. So you're not comparing one value with one. Instead you can give a full blown condition like that. So any condition can be given. This can be something like a greater than 10. Or it can be something like salary is greater than 10,000 are multiple conditions combined with logical operators, anything you like.

And then it returns one of these values based on the condition. Again, do remember, the first condition that is true is going to stop the whole process. If this fails, but this succeeds, we returned result two, that's where it stops. So eventually it is going to return a value but the way the comparison is done Different in case with selector and without selector. Then the next one is a small example. This is where I want to find out what is the bonus of employ 120 how much bonus are we giving to employ 120 It depends on the salary of the employee.

So, get the salary of employ 120 into variable. Well as usual we declared that variable with the same data type as salary and bonus person taste is a variable of number of five. Now, take this case this is what we have to use. So, we are saying if salary is less than 5,020% if salary is less than 10,000 that means it is in between 5000 to 10,000 Then I get 15 If not, we go further down is this salary less than 15,000? We already checked for 10. So this is effectively asking is it in the range 10,000 to 15,000.

If so, eight returns 10 and if none of these conditions satisfied, then it goes to ELS and returns five whatever it returns this whole case expression is returning a value and that goes into this variable. And now I want to display the value. We are saying bonus percentages for employ 120 ease and we are giving the value and this is our string concatenation operator. Of course we can use it even in PL SQL. And also do not forget whenever you are using port line with the DBMS app. But you need to first ensure you turn on server output.

Otherwise your program runs successfully, but no output is displayed. So, just be aware of that that is just an extra step. Then we have case statement, case statement is not returning a value instead taking an action, it is going to take an action, the same overall picture when condition then the statements in the previous case we were returning a value, but this time we execute some statements and we can have any number of such when clauses and if all of them fail, then it comes to else. Similarly, a slight modification to this is this, where we compare the case of operand with operate. This is like our case expression with the selector. How we compare selector with the different values, we do the same, the only difference is here we execute statements not just returning a value.

So, let us see an example for this. I take the salary for employee 120. And based on the salary of the employee 120, I'm going to display a message. So, if his salary is greater than 15,000, I say very costly employ. And if it is not greater than 15 in between 10 and 15, then I'm going to match this I'm going to satisfy this condition. Then I display a message says costly employ, and if the salary is not even greater than 10,000, then we disagree.

Played the message saying not so costly employ. So, this is how we can take salary of an employ and try to classify employ as very costly costly not so costly. So, we are using case statement in this context, in the previous cases we used case expression. So, in this lesson, we understood what to do with assignment operator, how our domestic operators work, what functions are allowed in PL SQL blocks, if statement and relational operators. Of course, you can always use logical operators as well. case expression and case statement.

There are subtle differences in between case expression and case statement. case expression always returns a value case expression takes action, execute some statements. And in both again we have two variants with selector without selector. So, we have seen the syntaxes a few examples, try to write some more programs using these elementary control structures. And we will see more about programming using PL SQL in the next lesson. See you there.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.