Cursor Handling

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.28
List Price:  €96.12
You save:  €28.84
£55.93
List Price:  £79.91
You save:  £23.97
CA$98.01
List Price:  CA$140.02
You save:  CA$42.01
A$107.87
List Price:  A$154.11
You save:  A$46.23
S$94.42
List Price:  S$134.90
You save:  S$40.47
HK$544.78
List Price:  HK$778.29
You save:  HK$233.51
CHF 62.22
List Price:  CHF 88.89
You save:  CHF 26.67
NOK kr779.46
List Price:  NOK kr1,113.56
You save:  NOK kr334.10
DKK kr501.79
List Price:  DKK kr716.88
You save:  DKK kr215.08
NZ$119.98
List Price:  NZ$171.41
You save:  NZ$51.43
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,402.58
List Price:  ৳12,004.20
You save:  ৳3,601.62
₹5,911.93
List Price:  ₹8,445.97
You save:  ₹2,534.04
RM312.68
List Price:  RM446.70
You save:  RM134.02
₦118,358.68
List Price:  ₦169,091.08
You save:  ₦50,732.40
₨19,451.98
List Price:  ₨27,789.74
You save:  ₨8,337.75
฿2,421.30
List Price:  ฿3,459.15
You save:  ฿1,037.85
₺2,419.75
List Price:  ₺3,456.94
You save:  ₺1,037.18
B$406.81
List Price:  B$581.19
You save:  B$174.37
R1,266.21
List Price:  R1,808.95
You save:  R542.74
Лв131.43
List Price:  Лв187.77
You save:  Лв56.33
₩98,455.31
List Price:  ₩140,656.47
You save:  ₩42,201.16
₪260.61
List Price:  ₪372.31
You save:  ₪111.70
₱4,125.84
List Price:  ₱5,894.31
You save:  ₱1,768.47
¥10,813.84
List Price:  ¥15,449
You save:  ¥4,635.16
MX$1,432.62
List Price:  MX$2,046.69
You save:  MX$614.07
QR256.35
List Price:  QR366.23
You save:  QR109.88
P959.91
List Price:  P1,371.36
You save:  P411.45
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,476.67
List Price:  E£4,966.88
You save:  E£1,490.21
ብር8,762.26
List Price:  ብር12,518.05
You save:  ብር3,755.79
Kz63,877.12
List Price:  Kz91,256.94
You save:  Kz27,379.82
CLP$68,152.06
List Price:  CLP$97,364.26
You save:  CLP$29,212.20
CN¥507.37
List Price:  CN¥724.85
You save:  CN¥217.47
RD$4,236.71
List Price:  RD$6,052.70
You save:  RD$1,815.99
DA9,355.50
List Price:  DA13,365.57
You save:  DA4,010.07
FJ$159.32
List Price:  FJ$227.62
You save:  FJ$68.29
Q542.77
List Price:  Q775.43
You save:  Q232.65
GY$14,710.67
List Price:  GY$21,016.15
You save:  GY$6,305.47
ISK kr9,775.50
List Price:  ISK kr13,965.60
You save:  ISK kr4,190.10
DH703.21
List Price:  DH1,004.63
You save:  DH301.41
L1,276.61
List Price:  L1,823.81
You save:  L547.19
ден4,112.73
List Price:  ден5,875.58
You save:  ден1,762.85
MOP$563.70
List Price:  MOP$805.33
You save:  MOP$241.62
N$1,272.29
List Price:  N$1,817.64
You save:  N$545.34
C$2,573.53
List Price:  C$3,676.63
You save:  C$1,103.10
रु9,461.06
List Price:  रु13,516.38
You save:  रु4,055.32
S/267.08
List Price:  S/381.56
You save:  S/114.48
K283.05
List Price:  K404.37
You save:  K121.32
SAR262.80
List Price:  SAR375.44
You save:  SAR112.64
ZK1,938.90
List Price:  ZK2,769.98
You save:  ZK831.07
L334.76
List Price:  L478.25
You save:  L143.49
Kč1,707.89
List Price:  Kč2,439.96
You save:  Kč732.06
Ft27,663.65
List Price:  Ft39,521.19
You save:  Ft11,857.54
SEK kr779.03
List Price:  SEK kr1,112.95
You save:  SEK kr333.92
ARS$70,272.32
List Price:  ARS$100,393.34
You save:  ARS$30,121.01
Bs485.83
List Price:  Bs694.07
You save:  Bs208.24
COP$310,661.31
List Price:  COP$443,820.90
You save:  COP$133,159.58
₡35,743.76
List Price:  ₡51,064.70
You save:  ₡15,320.94
L1,776.85
List Price:  L2,538.46
You save:  L761.61
₲551,865.53
List Price:  ₲788,413.13
You save:  ₲236,547.59
$U2,996.30
List Price:  $U4,280.61
You save:  $U1,284.31
zł292.33
List Price:  zł417.63
You save:  zł125.30
Already have an account? Log In

Transcript

Hi. In this lesson, we understand how to deal with cursors. Till now, we were restricted to retrieving only one row at a time, because select command could retrieve only one row, as it could copy the data into only one set of variables. But what if I want to retrieve multiple rows and do some process with each row? Well, the answer is a cursor. A cursor is where we can load data from the table and access those rows one by one.

We have two types of cursors implicit or explicit. Let us first use explicit cursor, because this is what we need to use to get multiple rows from the table into memory and process those rows one by one implicit cursor also plays a vital role, and it is providing information about the most recently executed SQL command. We'll see how implicit cursor can be used. Now, this is how explicit cursor is used. You read the data from database using a select statement into cursor. And then you will be taking one row from cursor, which is called the current row and copy the data from that row into some variables.

And then those variables can be used in the process. And your process starts with the first row. We keep moving down until we reach the end of the cursor. The whole process is very similar to how you open a file, read line by line and stop once you reach the end of the file. In the same manner, we read the data into cursor takes row by row process each row. And when we hit the end of the cursor, we stop the process.

So here are the steps we have to take. Number one, you declare the cursor. At the time of declaration, you give the name for cursor, what is the name we use to refer to it, and what is the SQL statement that you want to associate with the cursor? Well, of course, the statement is SELECT statement, and that is what we use to retrieve the data into cursor. And then you need to open the cursor. This is exactly where the select statement is executed, and the data is loaded into memory.

And then you need to fetch one row at a time into variables. So take one row from the cursor using fetch statement and copy the columns of the rows into variable You must know how many columns are there in the cursor, so that you can give appropriate variables after into. But you can't go on fetching forever. So you need to know whether you reach the end of the cursor. When you reach the end up the cursor, you're going to stop, but to know that you need to use an attribute called not found. So cursor name percent is not found, will tell you whether the previous fetch succeeded or failed.

If the previous fetch failed, then you're going to exit. So when not found a screw, you exit the loop. So the third step is iterative. You have to repeatedly execute those statements until exit is executed. And finally, always close the cursor once it is done. So let us put this Do a program.

Here is my program. I want to retrieve details about jobs. I want to take the details of jobs for which we have some employees. Well, it can be any query any simple or complex query. So step one, you're declaring a cursor with the name jobs cursor. The select command is retrieving job ID and title.

Then we create two variables, because we need to copy the data into those variables. Open the cursor, that's where the data is actually loaded into cursor. Then set a loop, fetch one row at a time. And we know the row contains two columns. And those two columns are copied into the job ID and job title. But we can't proceed if the previous fetch failed.

So find out whether fetch succeeded or not by using Jobs cursor, that's the name of the cursor person is not found. That is the attribute. That's the syntax to use attribute. So when not found is true, we stop. Otherwise, we just displayed the details. This is just a demo program.

But in place of the display, you can put any amount of process any amount of business logic related to that row. For example, you can take one employ at a time and find out how much increment the employee should get with lots of calculations and finally, update the details of the employ. So any amount of business logic can be put in place of this. This is just a demo. Our focus is on knowing how the pieces are to be put together. But once you know this, you can go and do that on a large scale.

Once you exit the loop, close the cursor. So declare, open fetch, and close, these are four important steps. Now let us run the program and see how it works. So here is my program. The program is same as what we discussed. And we are going to display the details.

And here is the query. You can have anything you like as long as you are consistent with the columns and the variables you're using. Now go ahead and run that. It's going to display all the titles for which we have some employees. And you can change that to something simpler if you like. I want to remove all that.

I want to just get all the jobs, that's fine. Go ahead, run and you're going to see all the jobs, whatever jobs we have in Job stable, it's going to get you all. So you have to have these four steps, but this is a little lengthy. So PL SQL felt there must be a simpler way and they brought what is called as cursor for loop. So when it comes to cursor for loop, you have to declare the cursor exactly in the way you did before. So there is no change.

And I can simplify that if I like with this, that's fine. And then we don't open the cursor. We are not going to do all that stuff. The cursor for loop is specifically designed for loop. It is not expecting you to open the cursor, it does it for you. It's not expecting you to fetch rows and then check whether you reach the end of the cursor.

All that is done automatically. So it is going to take your cursor it'll read one row from cursor into this variable. And this is called a record variable. And you don't need to declare it, because this is automatically created by PL SQL. And if you see what it has, it has two fields, it's going to have the job ID, because that's what we have and the job title. And we can refer to job ID in this way, job record dot job ID.

And the second one can be referred using job record that job title, so no need to open the cursor, no need to fetch rows yourself and check whether you reach the end of the cursor. Instead, your for loop is going to do that. It will execute the body of the loop for each row by copying the row into this variable so you can get access to Job ID and job title in this way. And finally at the end of the whole process, it stops the loop, and it closes the cursor. So it does the same as before, there is no change in the way The output is displayed. So if you go run this, you'll get the same result.

But only thing is you're not doing all that stuff that you did before. Well, if it is suitable to your requirement, if you feel cursor for loop is fine, go ahead and do that. Now, this is about the cursor for loop we already discussed about it. The syntax is simple row type variable, but you don't need to declare that cursor name and then the statements whatever statements you like, and this opens the cursor fetches the rows terminating the loop automatically when you reach it, end up the cursor and closing it. So this is the best thing. option if you are dealing with the cursor.

Now, the cursor for loop as we discussed is using a record variable and you have to access the columns using the record variable in this way, this is what we have seen already. Now, cursor is having four attributes, these attributes are called cursor attributes and they are slightly different for implicit and explicit cursor. So, not found returns true if previous fetch failed, found returns true if it succeeded. Row count tells you how many rows were fetched so far it's a useful one and is open tells you where the cursor is open or closed. Here is a program that sprains Levante to 15 the highest salaried employees Want to get employees who are drawing the 11th to 15th, the highest salaries. So I'm going to pick up all the employees in the descending order of salary.

And I go with the cursor for loop. And I say, if the count is greater than 10, then I want to display the name. That means we fetch 11 rows, this is 11th row. And if it is the 15th row, that means 11 to 15 you already displayed and you want to exit the loop. So this is just a small example to demonstrate how you can use a row count to know how many rows were fetched. By understanding these possibilities, you will be able to use them in your business requirements.

Then comes the implicit cursor. So explicit cursor is all about retrieving multiple rows and storing them in memory and accessing those rows one by one. But implicit cursor is all about information. It provides information about what happened with the most recent SQL command expecially commands like update and delete, because it can tell you how many rows were affected by those commands, whether any row was affected or not. So here is the example. After the update command, I want to know how many rows were affected.

And if it is updating more than 10 rows, I don't want to proceed with this update. I say roll back. Otherwise I say commit the change. So I can know how many rows were affected by an update command. Similarly, I'm deleting a row, but I don't know whether I could really delete do remember In the previous lesson we discussed, update and delete, do not raise any node data found exception, even though no row is affected. So you don't get any exception even if 130 is not found.

This is the way we can find out. If sequel percent is found is true. That means we found a row and we deleted that we go ahead and commit. So sequel percent is the attribute. The implicit cursor plays a very important role in the context of detecting what happened with update and delete because they don't otherwise tell you what exactly happened. Then, this is a slightly advanced feature associated with cursors.

You can create a cursor with input argument. I want to create a cursor that is Going to get implies for a particular job, but I don't know which job I want the open statement to provide the job. So, at the time of you opening this cursor, you can give a value and this is copied into the parameter and then that parameter is used in our select command. Do remember, at the time of declaration, the statement is not actually executed, it is executed only when you open the cursor. So this query is executed only at the time of view opening the cursor. So if you provide the input that is used there, and it will get all the employees where job ID is it programmer, in case if you are using not open but cursor for Lou, then right after the name of the curse You need to give the parameters in parenthesis.

So this is the parameter we want to pass to the p underscore job ID of your cursor. Well, you can have multiple parameters if you like. So let us see an example for that. Well, this is the previous program where I discussed about displaying 11th to 15th. Go ahead and run, you're going to see the employees, those are the 11th to 15th, the highest paid employees. And this is about our cursor with input argument.

And this is where I'm passing it programmer, the cursor is opened, I can do some process and I can close it. And if I'm using cursor for loop, do this in this way. And whatever might be the case, you can process the data. So if I run this program I'm going to get the employees from IT department. Well, I should not select the line, I should select the whole thing. So there you are.

These are the it programmers. But if I change this from it programmers to sales managers and run that you're going to see a different set of people. So those are the sales managers in your employees table. So, this is the way it is possible for us to pass some parameters to cursor. This is about cursors. And we can create a cursor to accommodate multiple rows retrieved by a query.

Implicit cursor plays important role in knowing what happened with update and delete because it tells you how many rows were affected, whether any row was affected, etc. cursor for loop is the best way to handle it. cursor, because it minimizes your burden. You can have an argument to cursor so that when you open the cursor, you can provide some input. And cursor can bring different sets of rows, depending on the input. So that's about cursor for now.

And you can use cursor in any context. So later, you can use it in procedures, functions, triggers. In fact, anywhere you can use PL SQL, you can use cursors. So that's for now about cursors. 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.