Programs Units - Part 2

26 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 go into more details of programming units. We understand more about procedures, functions, and also bringing a new object called package. And we understand primarily more about how to deal with parameters in the context of procedures and functions. Let's get started. We have seen already how to pass parameters in the previous lesson.

And now, the parameters are of three different modes. So when you create a parameter, if you don't mention anything about the parameter mode, it's considered to be in which is the default parameter mode. But you may want to have parameters which are of mode output that is specified you using the keyboard out, you may also want to have parameters which are both in and out. So, what do I mean by out parameter in parameter is saying we are sending a value into the procedure out parameter is saying the value is coming outside the processor. So, the processor sends a value to the caller using out parameter in out parameter is saying it is going to receive a value from the caller and it is also sending a value back that means, the actual parameter which is what we are sending from the caller will be assigned to the formal parameter, the parameter that we declare in procedure and if you receive the value from actual to formal We call it as in, but if you are sending the data from your formal parameter back to actual parameter, it is out.

And if you want to do both it is called as in out. So let's examine these options in parameter is the default. So you don't need to mention it and it is going to receive the value from the actual parameter. So here formal parameter is acting like a constant. So the parameter we declared in the procedure is working like a constant you can't change it. And formal parameter cannot be assigned a value because it behaves like a constant.

And actual parameter means whatever we are passing to formal parameter can be a constant or it can be any variable or an expression whereas, in case of an out parameter, you have to provide not any constant the actual parameter must be a variable, it cannot be an expression or a constant and formal parameter must be assigned a value because the value is coming back from the procedure to the caller and formal parameter behaves like an uninitialized variable. So, it is like a variable that is not assigned any value. So, we need to make sure we assign a value to that formal parameter inside the procedure. Only then the value can go back to actual parameter and in out is actually the best of these two. So here you need to make And in out very specifically, even that is the case for out because default is in passes a value in both ways.

So the actual parameter is copied to formal parameter changes made too formal parameter can go back to actual parameter. And in this case formal parameter is an initialized variable. And then actual parameter must be a variable because its value is copied to formal parameter and the value from formal parameter goes back to actual parameter. An example would make the whole process easy. So let's look at this. I have a procedure that is going to get me the minimum and maximum salaries for the given job.

So it is going to take A job ID as in parameter. And I don't need to mention it, but it doesn't harm if I mentioned it explicitly. The next thing is out parameter and I must explicitly gave out here and there. So both these are considered to be uninitialized variables, and they must be assigned a value in the body of the processor. And if you don't assign the value, it's considered to be an error. So minimum salary, maximum salary are called out parameters.

And all three are called formal parameters. Please be aware of this terminology. And we use the same terminology in many other languages. It's not anything specific to PL SQL. So job ID is what we are receiving from the caller. We get this as parameter and then we are talking about two out parameters.

One is minimum salary, the other one maximum salary. Now, this query is going to retrieve the data and copying that into those two out parameters. That's all. That's all we need to do in the procedure. Once the procedure is done, you are guaranteed values for minimum salary and maximum salary. Then how do you call this procedure?

Well, here it is. to call the procedure we need to provide three parameters. The first parameter can be any constant or a variable does not matter. But second, and third parameters must be variables, because those two parameters are out parameters. When you're dealing without parameter, the actual parameter must be aware of Remember this is this is what we call as actual parameter, this is the formal parameter. So, it is going to receive the value from your formal parameter to corresponding actual parameter.

That is what out is doing. By any chance if you want to send data from this actual parameter to formal parameter, and also receive the data back from that today's you should declare that as in out just out won't do, because we want to receive something from actual parameter and send something back to actual parameter. So, this is what we mean by in our parameter. So, here we are, let's execute the code and see how things go. This is our procedure So let's go create the procedure. And procedure is successfully compiled means created.

And the next step is to run that, to run that procedure to invoke that we have to give this and to print the output that is coming in to minimum and maximum salary variables, we need to first start this, set the server output on as usual, and then go run this and as you run, you're going to see the output there. So the processor is giving the output 4000 209,000. Let's clear everything and then run it again and you can see the output 4000 209,000. That's how it works. And you have to be careful about what to use here whether out are in out, are in it's not such a difference. decision, but still you need to be aware of the implications.

So, if you have any doubt go to this chart there you get all the details. Now, the next interesting thing is parameter notation. So how do you pass values to parameter? Not that whether you can modify or not modify, but how do you pass values to parameter? Let's understand an example first, I have now three parameters for my procedure. The first parameter is n one and we are assigning a value to that which is called as default value.

So, we have a facility called default value. That means, if no value is provided to this parameter at the time you call the procedure I want one to be assigned to that this is called default value. The syntax is simple, you need to give this assignment operator followed by the default value. So one will be assigned to N One, two will be assigned to N two, and three, four n three. That's how we designed this procedure. Now, the first invocation is where we pass three values to those three parameters.

So and to get 20 entry gates that the N one is 10. So those three are passed based on the position. That means first value goes to first parameter, second value goes to second parameter. This is what we have been using right from the beginning. And right from the beginning racquel pl SQL supported this, but as of late, we got some nice new additions where you can pass parameters by giving the name of the parameter. So I don't need to know the order of the parameters.

For example, I don't know whether n two is given first or third position or in fourth position. So I can just directly give into and gave the value. And again, you need to use a different operator. And this is known as fat arrow. It's just equal to followed by greater than, so give the parameter name and give the value the same I'm doing for entry. What does it mean?

It means your n one is not given any value, that's fine because we already have a default value for n one. Of course, you can't ignore any parameter that does not have a default value. So if you have a default value, that is fine. So you If you have that default value, then that will be automatically assigned to anyone. Now enter gets 20 and three gets 30. So, the output is going to be one and then 20 and then 30.

Now comes the last option, where we are mixing positional parameter notation with the name of the parameter notation. So, what does it mean? The first parameter is not using any name, so, it goes to the first parameter here. So, 10 goes to N 1am not giving anything for n 2am giving a value for n three that directly goes there. So, some parameters are given by the position and the remaining are given by the name. This is called mix it notation and the advantage with the positional AES, you don't need to remember or don't need to know anything about parameter names, but you got to know the order of parameters.

And the advantage with the name, you don't need to worry about the position of the parameters, you just need to know the names of the parameters. And they might be in any order in the procedure. And then if you have a lot of parameters like say 10 parameters, and you want to give first three, so you don't need to bother about giving the name directly give the first three values for reminding you have to use if you like, the name of the parameter notation, that means you provide the name instead of position. So you are free to mix it. But if you're planning to mix it, do not forget you need to give all positional parameters first Then only named parameters. That is the order you need to go with.

So let's see how that works. And I have just a different example here. So this is where we create a procedure. Let's create it first. And let's just clear everything. And also we don't need the log, just push it down.

And now let's go create it, the procedure is created. And now it's time to call that and if you call this in this way, let's comment out some of them just to understand one by one. So if you go and run this, that's the output of it, the output is going to be 10 2030. And then if you try to run the same thing in this way, now I comment out that and then uncomment this, I gave 22 and two and then 32 and three and if I go and run this piece of code, I'm going to get 120 30. Of course, I can change the order, I can say something like n one is equal to 30. And then you know, the result is going to be different.

Because we are passing values to N one, and two, but not to entry. So the output will be 3020, and three, So play around and understand what's really happening. And then the last two is where we are talking about positional place the named so 10 goes to N one, and 10 goes to N three, and n two is not given a value. So it assumes automatically the default, which is two. So go run it, you're going to say 10 to 10. But you have to be careful as I mentioned, you have to give them in the correct order and if you're trying to do it In this way, what happens, you're going to get an error, a positional parameter might not follow a named Association.

So, it is saying a positional parameter Association cannot be given after a named Association. So all positional parameters should be given first and only then your named parameters can be used like this, whatever you want to give, okay, so when you have a procedure with a lot of parameters, like 10, sometimes 15, then it is very useful to go with named parameters, especially if you're not planning to provide values for all of them. So, whatever is your case, these are the features of the language. So, try to make the most of it. And also make sure you have a default value because, by any chance if you don't have a default value, then every invocation must make sure it is including a value for anyone. For example, if I recreate my procedure with days, and then if I try to call this, it's not going to work, because we are saying a wrong number of types of arguments, because we are not passing any value to the mandatory parameter.

The mandatory parameter here is n one. So you must give value for n one and then you can give value for any other parameter. So this is now Fine. Let's play it this and run this. And what if you're trying to give this using named parameter like this is that fine? Well, as long as the value is given, whether you use positional or named, it should not make any difference.

So that's the way the positional parameters work. Let's get back to our slides. And here. The next important thing is Invoker rights. We already understood what is defined and right. Define a right is where you run the procedure with the privileges of definer.

That means, whoever is the owner of the procedure, you will be invoking procedure with the privileges of the owner. But sometimes it is designed to have the privileges of Invoker to be used rather than the define that so let's look at this scenario. User two is having privilege to run procedure, which is created by user one. So procedure belongs to user one, but the user two is trying to enter Bakit when user two is invoking procedure, in normal cases, you get the privileges of user one, because user two is invoking the procedure created by user one, a runs the processor with the privileges of user one. So, in that case, whenever user two is invoking the procedure, procedure is using table that is belonging to user one, because procedure is run with the privileges of user one. But now I want it to be different.

I want the procedure to use the schema objects of the Invoker. So when user two is invoking the procedure, I want procedure to use the object of user two. And when the procedure is invoked by users One, I want the procedure to use the table belonging to user one. So in case if you have user three, invoking the procedure, it should use the table that is present in user three, not in user one. Well, if that is your requirement, yes, you can achieve that. And this is possible by using what is called as Invoker rights.

Here is an example for Invoker rights. I'm creating a procedure called add message. And this procedure is associated with the order ID current user. And by giving this you're telling a racquel that this procedure should be called with privileges of Invoker not the privileges of definer. If you don't give that class then the default is define a right so now add message is invoke it with the privileges of the caller. So if the procedure is using any table, the table is taken from the schema of invoke, not the definer.

So that's about Invoker rights and how it is different from definer rights. Well, when we have a lot of procedures and functions, sometimes even hundreds of procedures and functions, then we want to divide them into different groups so that we can more easily manage them. Also, at times, we want to have some variables and some artifacts that are to be accessed all over the application. In such cases, we can bring a new concept called package into picture. So a package is a collection of procedures and functions generally related to a particular topic. So I can create a package related to all the procedures and functions of jobs.

And another package for employs another package for departments and so on. A package can also have variables and data types that you can use globally. That means you can use some variables. those variables can be used even outside the package just like global variables. A package is having two different steps. You create the specification, first list out all the public members, public members are the ones we can access from outside the package.

Then you create the body where you define those public members. But you're also free to have some private members, the members that are not accessible outside the package. So here is my package is called department package department package is a package that is having two members, they're called as members. The first one is changement as a procedure, the second one is employee count which is a function. So, to create the package, we start by saying create package and followed by the package name. And within that we need to give the list of public members.

So public members are declared in pack a specification, and then they are to be created in package body. Create package body is another command and here is the example. So we are writing code for Change Manager and employee count and these are the members of the package. In addition to these two if you want, you can have some more procedures and functions, but they cannot be invoked outside the package. There's nothing new about the procedures and functions. They're very similar to what we have been creating, but they are just now part of the package.

And then if you see how we call those procedures and functions, just a small extra step, you need to use the package name when you call the procedure or a function that is part of the package. So I have to use DPT pkg that's the name of the package, followed by dot followed by the member of the package. Apart from the packages that we created, we can use a lot of packages provided by Oracle. So Oracle provides a lot of predefined packages that are called Oracle supplied packages and You see we have been using one such package called DBMS OUTPUT. So DBMS OUTPUT is a package provided by Oracle. Apart from that we have packages like DBMS refresh, which is used in the context of materialized views DBMS SQL is used for advanced dynamic SQL lob, which is used for lobs row ID DBMS.

Row ID is to manipulate row ID. And then utl is about utility packages utl file is very interesting pack is providing lots of functions and procedures using which you can manipulate files. For more information about the utl file, please check out my blog in my website. There I explained how to use utl file. So this is About program units part two. In this we talked about how to use parameter mods, parameter notations, how to deal with the Invoker rights and how is it different from definer rights.

And we created a package with the two different steps package specification first, then package body, we can create packages anytime we like. And Oracle also supplies a lot of packages providing various functionalities. So this is about programming units. It's a very important topic in the context of PL SQL. So do practice a lot. And I'll catch you with more about programming with PL SQL in the next lesson.

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.