Introduction to PL/SQL

15 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 what is PL SQL? And how do we use it to deal with data processing. Till now we understood how to deal with a data manipulation, data definition, data retrieval, and even how to control access to data. But all these things were done using SQL, the non procedural language, it's time to move into procedural language called PL SQL. This is the procedural extension to SQL.

Oracle is providing this language so that you can write programs which make use of variables, conditional statements, looping structures, exception handling, and all programming artifacts, we use days to do data processing. We create PL SQL programs in the form of blocks. And the blocks might be anonymous blocks without any name, and they're just executed. But the most popular blogs or sub programs, which are in the form of procedures and functions, and they are stored in the database and they are executed in the database. And when the procedures and functions are stored in the database, they are called as stored procedures and stored functions. And we also have triggers which are used to implement business rules, complex business rules, which cannot be implemented using integrity constraints.

So PL SQL plays a vital role in the context of Oracle and PL SQL is an integrated part of Oracle. When you run a PL SQL block as you can see here, it contains two different types of statements, procedural statements and the normal SQL statements. All these procedural statements are executed using procedural statement executed. This is part of Oracle instance. But this can be part of even some other environments like Oracle Forms and we are not talking about Oracle Forms in this course. So, we focus on procedural statement executed that is part of Oracle instance.

When it comes to SQL statements, they are always executed in your database server and It is always executed by SQL statement executed. So, every block is consisting of some procedural statements, some non procedural statements namely SQL statements. Let us see how we can create a block, this is anonymous block a block without any name and you can just straight away Run this block, whether it is anonymous block or a stored procedure or a function. Every block is having a few important parts like declare a two part, executable part, Exception Handling part. declarative part is where we declare variables and also cursors and exceptions later. And executable part is where you put executable statements and they stay domains could be SQL statements as well as PL SQL statements.

Exception handling is where you handle the exceptions that are raised by executable statements. When statements fail, they raise exceptions. And those exceptions are handled by exception handler, which is an important part. Because when exceptions are not handled, your blog is unsuccessful, it is considered to be unsuccessful. So it's important to have exception handler so that we can handle exceptions. And here is an example for declarative part.

The declarative part is all about declaring variables. And each variable is having a data type. That can be one of the standard data types like number date, where can And so on, but more often we prefer to declare variables with the same type as a column in the table. If the column in the table is first name, and if you want the variable to have the same data type as first name of employees, then we have to use something called person test type attribute to get that type of first name and that type is used to declare this variable. I just have a convention of using v underscore as the prefix for variables, but you can follow any convention you like. So this is where we declare variables and you can even initialize variables by assigning a value and do remember the operator is different it is called an equal to because that is the end assignment operator in PL SQL.

Well, pl SQL just like SQL is not case sensitive. So you don't have to worry about giving it either specifically in uppercase or lowercase. It's up to you. Now, these are the data types we have the scalar data types are same, the only new thing is Boolean. Boolean is not available in SQL. You can't create a column of type Boolean, but you can create a variable of type Boolean.

And then we have some other data types which are more specific to PL SQL like composite types, reference types and lobs are again common to both SQL and PL SQL. Now here are the different options available when it comes to giving comments. So if you want to give any remarks In your program, you can do it in the old style, I mean the C style slash star star slash R, you can just give a comment with the two hyphens, whichever you like. Then select command in SQL is retrieving the data. And we have seen in all our examples in SQL, it was displaying the result on the screen. But Strictly speaking, select is not meant to display the data.

It is only meant to retrieve the data and our tools like SQL plus or SQL Developer, retrieve the data using select and display the data in its own way. You can see the output is displayed slightly differently in SQL plus compared with SQL Developer. It's because select is only about retrieving data, it's not concerned with the display, the display is actually done by the tool. Now, in the context of PL SQL, select cannot display. It is only retrieving data. And whatever data select retrieves, we need to copy that into variables.

So that we can use those variables in further process. And if you use select without into, then whatever data you retrieve, it doesn't go anywhere. So make sure you understand how to use into it's pretty simple. And here is an example. I want to find out the salary and job ID of employ 110. And I want to retrieve those two details and they must be copied into these two variables and that is done using a new class called into.

And if you don't do this, then these two values go nowhere. They can't be displayed, and they can't be stored anywhere. So in the context of PL SQL, select always comes with into. And this is where you give the variables. So salary goes into this job, it goes into it. And it's better you declare those variables with the same type as the column.

So we typically do this salary is of type salary column of employees. Job ID is of the type job ID of employees. So these are some fundamentals you need to remember when you're dealing with PL SQL. So what type of programs can be right here is a small demo program. The program is finding out The name of the employ who is drawing the maximum salary. While it might be a trivial program, but it does demonstrate the steps involved in writing a PL SQL program.

To start with, you need to declare required variables. So these are the variables I'm interested in, then I want to take the maximum salary of all the employees and copy that into this variable. And we'll be using this variable in further process. The next step is finding out the name of the employ, who is drawing that maximum salary. So where salary is equal to V max salary, but there is one limitation, this does not succeed if you have more than one employ, drawing the same maximum salary, but with the assumption that is not so I go ahead with this. So, I pick up the name of the employer and put it into this and this is to be done only when the salary of the employ is equal into maximum salary.

Fine, we got the data copied into this this is important. Now, what do you do with that? Now that name is to be displayed. So this is where we display the message saying employ with the highest salary is signed. So to display anything onto screen in PL SQL, you need to use a DBMS OUTPUT dot put line you will understand more about this particular structure. But this is called a package and this is a processor.

So for now, you can say it's just like any other ready made function to take an action and the action taken by this is printing this message But one very important step is this you have to set server output to on if you want the output from DBMS OUTPUT to come. So let's switch and then run the program and here is the program and I want to first make sure this is turned on without that the output will not come on to the screen and then we are finding out the maximum salary and then finding out the name of the employee with the highest salary. And finally we display this message. Now let us select the whole thing and run and it is going to give you the message like this. It is saying employ with the highest salary is Stephen and he is the one with the highest salary.

And you can see the message saying your PL SQL procedure successful completed. That's what it says when the Pl SQL block is successful. But if you by any chance remote days, if you're not copying that into the variable, let's see what happens. Now go ahead and run. and it is saying n into is expected with select because if the intent is not there, there is no way you can store the selected value anywhere. So in the context of PL SQL, whenever you select, you have to retrieve the data copied that into one or more variables.

Well, if you're wondering what if I retrieve multiple rows, not just one? Well, there is an exclusive lesson about it. It's called cursor handling. We'll discuss about that in another lesson. But for now, this is how you get started. So just declared the required variables.

Retrieve the required details and then print whatever you want to print onto the screen. Of course, you can use update command you can use delete, all DML commands are allowed. So in this lesson, we understood what is a PL SQL block? How do we deal with that? We understood three different parts declaration, executable body, and then exception handling. So, this is about PL SQL.

We just got started long way to go. But you need to understand how you execute a PL SQL block, just put the block in SQL Developer and run it and then you have to have begin and at least if you have variables, you can have a declared section, exception and handling and other things will come later. That's all for now. 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.