Program Units - Part 1

25 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
€60.73
List Price:  €86.76
You save:  €26.03
£52.26
List Price:  £74.67
You save:  £22.40
CA$96.65
List Price:  CA$138.09
You save:  CA$41.43
A$108.94
List Price:  A$155.63
You save:  A$46.69
S$91.27
List Price:  S$130.40
You save:  S$39.12
HK$543.17
List Price:  HK$775.99
You save:  HK$232.82
CHF 56.49
List Price:  CHF 80.70
You save:  CHF 24.21
NOK kr728.92
List Price:  NOK kr1,041.35
You save:  NOK kr312.43
DKK kr453.48
List Price:  DKK kr647.86
You save:  DKK kr194.37
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,499.86
List Price:  ৳12,143.18
You save:  ৳3,643.32
₹5,958.92
List Price:  ₹8,513.11
You save:  ₹2,554.18
RM305.85
List Price:  RM436.95
You save:  RM131.10
₦112,301.75
List Price:  ₦160,437.95
You save:  ₦48,136.20
₨19,699.56
List Price:  ₨28,143.44
You save:  ₨8,443.87
฿2,315.75
List Price:  ฿3,308.36
You save:  ฿992.61
₺2,673.74
List Price:  ₺3,819.79
You save:  ₺1,146.05
B$415.18
List Price:  B$593.14
You save:  B$177.96
R1,307.79
List Price:  R1,868.36
You save:  R560.56
Лв120.48
List Price:  Лв172.12
You save:  Лв51.64
₩99,329.46
List Price:  ₩141,905.31
You save:  ₩42,575.85
₪258.20
List Price:  ₪368.88
You save:  ₪110.67
₱3,959.29
List Price:  ₱5,656.38
You save:  ₱1,697.08
¥9,867.68
List Price:  ¥14,097.29
You save:  ¥4,229.61
MX$1,375.77
List Price:  MX$1,965.48
You save:  MX$589.70
QR254.99
List Price:  QR364.29
You save:  QR109.29
P964.11
List Price:  P1,377.36
You save:  P413.25
KSh9,072.69
List Price:  KSh12,961.55
You save:  KSh3,888.85
E£3,531.11
List Price:  E£5,044.66
You save:  E£1,513.55
ብር9,325.92
List Price:  ብር13,323.32
You save:  ብር3,997.39
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$67,726.52
List Price:  CLP$96,756.32
You save:  CLP$29,029.80
CN¥509.94
List Price:  CN¥728.52
You save:  CN¥218.58
RD$4,218.99
List Price:  RD$6,027.39
You save:  RD$1,808.40
DA9,284.08
List Price:  DA13,263.54
You save:  DA3,979.46
FJ$159.98
List Price:  FJ$228.55
You save:  FJ$68.57
Q539.02
List Price:  Q770.07
You save:  Q231.04
GY$14,659.61
List Price:  GY$20,943.20
You save:  GY$6,283.59
ISK kr8,813.14
List Price:  ISK kr12,590.74
You save:  ISK kr3,777.60
DH646.94
List Price:  DH924.24
You save:  DH277.30
L1,210.80
List Price:  L1,729.79
You save:  L518.98
ден3,755.86
List Price:  ден5,365.75
You save:  ден1,609.88
MOP$559.47
List Price:  MOP$799.27
You save:  MOP$239.80
N$1,307.97
List Price:  N$1,868.61
You save:  N$560.64
C$2,573.90
List Price:  C$3,677.16
You save:  C$1,103.25
रु9,537.71
List Price:  रु13,625.88
You save:  रु4,088.17
S/261.88
List Price:  S/374.14
You save:  S/112.25
K289.26
List Price:  K413.25
You save:  K123.99
SAR262.61
List Price:  SAR375.18
You save:  SAR112.56
ZK1,995.15
List Price:  ZK2,850.34
You save:  ZK855.18
L302.11
List Price:  L431.61
You save:  L129.49
Kč1,521.65
List Price:  Kč2,173.88
You save:  Kč652.23
Ft24,736.02
List Price:  Ft35,338.69
You save:  Ft10,602.66
SEK kr665.84
List Price:  SEK kr951.25
You save:  SEK kr285.40
ARS$78,594.19
List Price:  ARS$112,282.22
You save:  ARS$33,688.03
Bs484.19
List Price:  Bs691.72
You save:  Bs207.53
COP$297,190.71
List Price:  COP$424,576.35
You save:  COP$127,385.64
₡35,158.13
List Price:  ₡50,228.06
You save:  ₡15,069.92
L1,810.73
List Price:  L2,586.88
You save:  L776.14
₲559,780.71
List Price:  ₲799,721.01
You save:  ₲239,940.30
$U2,963.96
List Price:  $U4,234.41
You save:  $U1,270.45
zł259.77
List Price:  zł371.12
You save:  zł111.34
Already have an account? Log In

Transcript

Hi. In this lesson, let's talk about how to deal with stored procedures. So what is this stored procedure? And how do we create it? a stored procedure is a PL SQL block that is given a name and it is stored in the database. We can call this procedure with the given name.

And it is meant to perform an operation in our business application. It is used to implement business logic. For example, we can use it to raise the salary of an employee. But while you're raising the salary, there might be a lot of operations to perform. There might be many business rules to implement, all that will be taken care by stored procedure from our applications like Java, Python, C sharp which called the stored procedure and the rest of the operations are performed in the stored procedure. So stored procedure is used to centralize our business logic in the database.

Now the syntax is pretty simple. We have to use create procedure followed by the name of the procedure. procedure may take parameters. If you have parameters listed out the parameters in parenthesis, and if you use or replace, you're saying if the procedure is already present, replace the existing one with the new version. If the procedure is not there, it is created. So generally it's a good practice to use or replace so that if the procedure is already there, you create the new version.

If it is not there, a new procedure is created and then Then comes the local declarations, they are the variables or other artifacts like exceptions cursors that you want to use in the stored procedure and then the executable statements followed by optional exception handler. You can handle exceptions within the procedure. So, let us see how to create a procedure. But before that, we want to know what are the advantages of a stored procedure. There are four significant advantages with stored procedure number one, it reduces the network traffic because from our application we are going to call stored procedure only by its name. We are not going to send the whole data we are not going to pass any PL SQL block from client application to database.

Because the stored procedure is there within the database, all that you need to do is just simply send the name from application to database, the procedure is executed in the database. This saves a lot of network traffic. Number two procedures generally run faster than the corresponding PL SQL block for two reasons. Number one, procedures are compiled and stored. When they are stored in the database, they are first compiled. Number two, once the procedure is executed, it's loaded into memory and Oracle stores in the memory.

So subsequent executions of procedure will not need to load the processor again from the database. They are already there in the memory so The procedure runs faster second time onwards, because it need not be loaded from database into memory. So combining these two, we are making the procedures much faster than the equal and PL SQL block. easy maintenance number three, it's because we have the whole business logic centralized in the form of procedures in the database. If I have to change any of those business rules or business logic, all that I need to do is just go to my procedure change it once all my applications will be immediately affected by the change in the stored procedure, because all applications are designed to invoke stored procedures. For example, if I have to log the change to salary of the employ, then I can no incorporate that new feature in the procedure.

And whenever you call the processor, automatically logging takes place because the procedure is modified. We don't need to change anything in our applications like our Java application and other applications. Finally, the fourth point is security. Instead of giving other users access to the tables views, we now give them procedures which internally use those tables and views. Let's look at this diagram. Normally, you would give users access to the table if you want user to make any changes.

But this time, we are not giving access to table. User two does not need any privilege on the table. Instead, he needs a privilege on the processor. A would call the processor and then the processor is going to make changes to the table. But how can you use that to make changes to a table that belongs to user one, when user two has no privilege on the table? The answer is, when user two is running the procedure, he runs the procedure as if he were user one.

In other words, user two is running the procedure with the privileges of user one. So when user two is invoking processor, or achill, takes user two as user one. Whatever privileges user one enjoys the same privileges user two is also going to enjoy but only when is running the processor. So, when user invokes processor is going to update the table because he is enjoying the privileges of user one. This is also known as a definer rights. So we use the rights of the definer of the procedure, that means the owner of the process.

So those are the four major advantages of using a procedure versus creating a normal PL SQL block. Now, let's talk about how to create a simple procedure called change salary. It takes two parameters, employee ID salary, it's updating the salary of the employee to the new salary, and if it is done, it is going to come it if you don't find employee ID, it is not going to do anything but you can change the assays that to raise an error. And we have seen in the previous lesson, there is a way to RAISE APPLICATION ERROR. And we generally use that in processors. So when the job is not done successfully, you might want to inform to the caller about the problem.

So you RAISE APPLICATION ERROR, we'll change this to understand how that is going to change the scenario. So let us switch to our SQL Developer and see how we create the procedure. So this is how we create the processor. So just execute this command and it is going to create a procedure and you must see the message just like this procedure signed, so compiled. And if you see any other message, it means your procedure is not successfully created. Let me make a small error.

I'm not using the right table name. And I'm trying to now create the procedure. Let's clear the screen and go there. And now you see, it is giving you error messages. And it is telling you that there are errors, the table or view does not exist. But you might not always get all these details, especially if you're using SQL plus to create procedures, you don't get to see all these error messages.

But never mind. You can still get all the details. But for now, this is what you need to know. So if you're not successful in creating processor, you get to see all these error messages. So check out what is word and go back and recreate the processor like this. So now the procedure is compiled with no errors.

So this is how we create a procedure And now how do you call the procedure. Once the procedure is created, there are two ways in which you can call the procedure number one, you can call the procedure using execute command. If you're calling the procedure from SQL class, you can give execute followed by procedure name and required parameters. And if you are calling it from another PL SQL block, you just need to give the name of the procedure followed by the parameters. And also if you are using execute privilege, this is the privilege we use to grant the execution privilege on the procedure to a user. So this is a privilege applicable or relevant only to program units like stored procedures and functions.

So I'm glad During executive privilege on change, salary to demo demo is the user and execute is the privilege and change salary is the name of the program unit that is our procedure. And then what if you don't succeed in creating the processor? What if there are some syntax errors, the first thing you can do is use show errors. By using show errors, you can display all the errors. This is the case if you're using SQL plus, but I strongly recommend use SQL Developer if you're creating more procedures because that's a more sophisticated environment. You can also get errors from user errors.

And this is going to give you errors for all the procedures but if you're very particular about one procedure, use name Then ask for that. So there, we say where name is equal to science. And then another important thing is getting the source code of a stored procedure. After you create that processor, if you ever want to know what is the source code, you can use user underscore source. That's the data dictionary view. And again, mentioned the name.

And don't forget, object names are always given in uppercase. We discussed about that in the past, do keep it on your mind. So you should give change salary in uppercase. Let's switch back and then let's change this. So if I'm not able to find the employ, then it doesn't do anything. Okay, let us first call this so I say begin.

And I say change salary, and I'm giving some numbers. And some salary, I'm giving some 7000 let's just run only this and clear all that. And you can even clear this log so that we have only what we need. And we can even close this. So just go ahead and run the procedure successfully completed. That means the salary for imply 120 should be 7000.

Let's check that select salary from employees where employee ID is 120. Let's go around and you should see 7000 Well, we are spot on. But now what if I gave something like 320 we don't have any employee with the 320. So try running it and you're going to see no error message. It says procedure successfully completed but that's misleading because we didn't have any employees. With id 320.

So we can make a small change here, we can put an else here and say, if the change is not made, obviously it indicates that the employee ID is not found, we can RAISE APPLICATION ERROR. So we say RAISE APPLICATION ERROR minus 2000, sorry, 20,200. And we can say error message is employee ID not found. So, let's recreate the procedure as it is create or replace, we can create the procedure again, it'll replace the existing one, the procedure compiled successfully. And now if you go and run this, you're going to see that error message and the message is clearly telling you it is in the procedure change salary, and the message is employee ID not found Also check out the number that is the number you have assigned. So, that is the purpose of Ray's APPLICATION ERROR in the context of stored procedures we use it very regularly.

So when the job is not done, you want to raise error and inform to the caller that there is a problem. Well, the caller is a PL SQL block and it can handle that error by using Exception Handling if it wants. So, this is how you can modify recreate, and then you can raise an error if the job is not done. So apart from processors, we also have another artifact called function. The difference is simple. As third function returns a value add function is always expected to return a single value View.

Other than that, it's exactly same as stored procedure. And one more important thing, you can see return statement to return the value. And also you need to use return to specify what type of value the function is expected to return, the function is returning this type of value. So you must give the return type and eventually return a value from the function. So let us see an example. This is a function that is going to return a number.

That's what we mentioned here. We mentioned that it returns a number and the name of the function is get total salary and it is taking department ID. And this is where we calculate the sum and we put it into variable and this is where we are going to return value. That's it. So one new thing is you mentioned the return type, and then you return the value. And how do we call it we can call it from any PL SQL block.

And you can call it event from normal select command. And as it is shown, I can say get me the department name and also the total salary for that department. So we pass the department ID from every row and we are going to get the total salary so we display both department name and total salary. So let us switch and see how this works. And here is my get total salary. I'm going to create this and here we are, it's done.

And now I can call it in two ways. I can have a normal PL SQL block and say DBMS underscore output and port line and I can call that function. So I just need to call the function with the get total salary of 10. And remember, this is not case sensitive. So you can give whichever case you like. And also don't forget set server output on.

So if you go ahead and run this, you're going to see the total, this is the total for department 10. And if you change it to department 60 and try the same, you're going to see a different output. So 28,800 and as we already discussed, you can as well use a normal select so I can say department name, get total salary of department ID from departments. So you are going to get that and we can say or drive by to. So you're going to sort everything by this and that in the descending God. Well, you can apply all your SQL knowledge.

So this is where you're going to get the data. Well, when we have the null values, we have a problem okay. But other than null values in if you see this is coming in the descending order. So sales is having the highest total salary, then shipping and so on. So this is how it goes. And you can modify this slightly and say, if at all This is null, we want to treat it as zero.

You can. So we know a function if you remember NVL. I say if total salary is null, I want to treat it as zero. So let's go and recreate and this time the function is going to return not now but zero. When we don't have any employee in the department and now you come here and execute this and you should be saying this time only the values and not nulls at the top and the nulls are now converted to zeros. So, they go at the bottom.

So, this is the way you can always modify your function according to your requirement and there is no scope for error. So, we are not going to raise any errors. Now, there is another function which is going to return a string where character and we don't mention the the size of the data type. This is one important point. When we use the parameters we are not expected to we are not supposed to mention the size or we don't say where character of 20 instead, we simply say it is where can That's it. So get implies takes a string and returns a string.

And here, we created a cursor, we can create a cursor like this. So a stored procedure or a function can create cursor can create exceptions can raise them, do all sorts of things. So let us create this. This is where I take all the employees who are in the given job. And I want to concatenate all the employee names, separating them with a comma. And that's what eventually I'm going to return.

And this is the loop where I take one name at a time, concatenate that to names and also put a comma at the end. That's arrays and they will be an extra comma at the end, and we are trimming it using our train. So now if you go and run this part, we create a new function. So get employees is created. And now you can call that whichever way you can. For example, we say select job ID.

And then we say get employees of job ID from jobs they can also say where job ID in select a job ID from employees. So we are only considering those jobs for which we have employees. So this is how we can execute a query that is using our get employees and we are going to now see the job ID and the details of employees. I can make it even more interesting by going with the job title. Now go ahead, run this and you will see or something like this. Too small While somewhere, it is too small.

So this 200 is not enough. And we need to increase this, let's go for 2000. That's fine, because we have enough space. And go ahead, run that that's a kind of truncation error. Now go run. And you're going to see something like this, you see, you have the job title, and then the employees who are doing the job like this.

So, you can read a cursor, you can deal with exceptions, all sorts of things can be done. So this is how we create a stored function. And you can see what are the functions available? What are the procedures available by going to your SQL developers three here on the left, and there you can see all the objects and now you can see these are the three different ones and There might be a few more I created already. And both functions and procedures, you can see them. And you can also double click on that to get more details about that, it's going to show you the function.

So you get the source code straight away if you do this, so that's the way it goes. a stored procedure and stored function are stored in the database with a name. And with the help of the name, we can invoke them and get the job done. And on both you need to have execute privilege if you're not the owner, and you run them with the privileges of the owner, which is called as definer rights. So in this lesson, we talked about how to deal with the stored procedures. We talked about how to deal with the stored functions.

We understood, execute privilege. We understood you can errors if you want using show errors, because when the procedure is not created successfully, you don't get all the errors in SQL plus expecially. So we can use show errors to get error messages and user source is use it to get access to source code, we can use select the text, that is a column where the whole text is stored, but you need to mention which procedure you are interested in. So this is just part one of our program units. In this we understood how to create procedure and function. But there is a lot more to come.

So we will see more about procedures, functions, and then a few more related objects in the next lesson. See you 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.