Using GOTO

Intellezy Trainers
A free video tutorial from Intellezy Trainers
Computer Training Specialists
4.4 instructor rating • 198 courses • 152,965 students

Learn more from the full course

Oracle PL/SQL Fundamentals vol. I & II

A complete introduction to PL/SQL database programming language

19:59:18 of on-demand video • Updated May 2020

  • Over 90 lectures and 20 hours of content
  • Prepare your enviornment
  • Introducing Database Program Units
  • Creating and Maintaining Packages
  • Using System-Supplied Packages
  • Creating and Maintaining Database Triggers
  • Implementing System Event Triggers
  • Work with Oracle database tools
  • Understand and work work language features
  • Work with DECLARE, BEGIN & EXCEPTION clause
  • Understand and use nested blocks
  • Use explicit cursors
English [Auto] Now that we understand the label it's time to understand the goto statement the goto statement is simply a job within your program execution. It's a means of you being able to go to a new location and typically you'll use a go to statement with some type of other condition like an if then else but think of it as nothing more than a jump within your application. That's all it is. There are rules applied to the GO TO statement that you must understand first a valid label name must be specified as the target. You have to have your labels first before you ever specify a GOTO statement. And when you use your go to statement that label has to exist or else you're going to get an error and it makes sense because if you think about it if you say go to some location and that location doesn't exist or the application doesn't know how can it go anywhere Rule number two the angle brackets are used for the label itself. It's not used for the goto. So when you specify go to space all you care about is the name itself. No brackets. Keep that in mind. And rule number three the goto statement simply identifies the name which kind of goes with the second one but you need to understand all aspects. And here's an example where we have our declare we have our begin and then there is a label called label name. So we have ours we have our angle brackets and it says label name in between. Down here you have your go to. So you say go to space. Label name and remember notice what's at the end of my goto statement. I have my semi-colon Terminator. So you need to have that as well. This is a command. So it literally goes right back up to where a label name is and starts processing there. So that is literally how you can think of a goto statement. Now there are a couple of other things you need to understand about a goto. It has some eccentricities that you need to be aware of. The label cannot be the last statement in APL sequel script so you can't just say go to the end and have some label name and nothing else. It can't say label and then have and if you do that you will get an error. You have to have something for it to do. So fortunately we have a command that we can put if we want it to simply just stop execution. And that is null. No is a valid command inside of Piel sequel. So right here we have an if statement we say if active EMP SSN equals the inactive EMP SS and then go to conclusion. So we say I want to go to this label called conclusion and down here we have conclusion. And right after that we just simply have the command called Null and then it says end. So no is not going to do anything but it keeps us from running into that error which is the label cannot be the last statement within our Piel sequel script. So keep that in mind if you put a label at the end without some command afterwards you will get an error and now you you'll be able remember oh I remember something from that course. You got to put some other command. And believe me the only reason I bring this up is because it's happened to me when I first started programming APL sequel. I had a label as the last command followed by and kept getting this error. Couldn't figure out what was going on. Had to put it all in and I was fine. So just trying to keep that from happening to you. So let's take a look at it. So I have a script out here called go to. So if you download that you can take a look at it with me but it's no different than this then the script we had before we have our declared statement we have our we have our declare for a type or redefining a type. We declare our variables. We have our locate active. We have our output active. We have our locate in active and then we have right here at line 43. It says test whether the active and inactive individuals are the same. So I'm literally executing an if then else. So if active empat SSN equals the inactive EMP SSN then go to conclusion. So literally there's only a true side to this. So if they don't match it will just go to the END-IF and keep on going. But if it if they do match it says I've got to go to this location called conclusion. And if I go down the very bottom I have conclusion right here. And I have my no statement right after this. So that's all we're doing with a go to. Now I want to point something out to you. The goto statement is perfect for a situation like this where you want to test for something and if the condition is true. I want to bypass everything else and instead of having a very very large if statement that branches over for who knows how far and makes it very difficult for you to process. You can use a go to stay but to simply say hey if this condition is true go to some location and terminate will be done and that's fine if you need to go back and forth. That's where we're going to start talking about looping and looping is going to be the next topic. But this is how you use the goto statement the labels and the goto's work hand-in-hand. They go together like peanut butter and jelly. So keep that in mind.