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.18
List Price:  €95.97
You save:  €28.79
£55.86
List Price:  £79.80
You save:  £23.94
CA$97.87
List Price:  CA$139.82
You save:  CA$41.95
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,267.89
List Price:  R1,811.35
You save:  R543.45
Лв131.37
List Price:  Лв187.69
You save:  Лв56.31
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪259.10
List Price:  ₪370.16
You save:  ₪111.06
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$69,057.73
List Price:  CLP$98,658.13
You save:  CLP$29,600.40
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,776.20
List Price:  ISK kr13,966.60
You save:  ISK kr4,190.40
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,259.44
List Price:  ARS$100,374.93
You save:  ARS$30,115.49
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
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.