Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,570 --> 00:00:05,790
In this lesson we're going to
be looking at PL/SQL procedures.
2
00:00:05,790 --> 00:00:09,690
So PL/SQL procedures
are distinct from things
3
00:00:09,690 --> 00:00:14,430
like anonymous block PL/SQL,
because it's actually compiled
4
00:00:14,430 --> 00:00:16,330
and stored in the database.
5
00:00:16,330 --> 00:00:19,050
So just as a table,
as a database object,
6
00:00:19,050 --> 00:00:22,480
a procedure is a
database object as well.
7
00:00:22,480 --> 00:00:25,260
And what this does
is it facilitates
8
00:00:25,260 --> 00:00:27,600
the use of shared code.
9
00:00:27,600 --> 00:00:31,680
So rather than having
PL/SQL scripts running out
10
00:00:31,680 --> 00:00:35,490
on a file system or
someone's desktop, when
11
00:00:35,490 --> 00:00:40,200
we create procedures and
compile them into the database,
12
00:00:40,200 --> 00:00:43,800
they're more easily accessible
by a number of processes,
13
00:00:43,800 --> 00:00:49,240
be it developers, users,
applications, or what have you.
14
00:00:49,240 --> 00:00:51,960
So what I want to do
first is show you just
15
00:00:51,960 --> 00:00:57,000
how easy it is to convert
anonymous block PL/SQL scripts
16
00:00:57,000 --> 00:00:59,260
into a procedure.
17
00:00:59,260 --> 00:01:02,700
So here we have an
existing anonymous block,
18
00:01:02,700 --> 00:01:05,490
declare section with the
variables and the cursors.
19
00:01:05,490 --> 00:01:07,620
The begin section opens.
20
00:01:07,620 --> 00:01:11,410
The cursor then does
the work, and then ends.
21
00:01:11,410 --> 00:01:18,650
In order to convert
this, I simply
22
00:01:18,650 --> 00:01:21,020
take the declare
statement and change
23
00:01:21,020 --> 00:01:23,600
it to create or
replace procedure,
24
00:01:23,600 --> 00:01:25,130
and then give it a name.
25
00:01:25,130 --> 00:01:30,620
And then the is defines where
the declarative section begins.
26
00:01:30,620 --> 00:01:32,990
And if I want to
create this object,
27
00:01:32,990 --> 00:01:39,410
I simply highlight and execute.
28
00:01:39,410 --> 00:01:41,720
And it shows that the
procedure employee
29
00:01:41,720 --> 00:01:43,060
report has been compiled.
30
00:01:45,710 --> 00:01:47,720
It's just really a
matter of convenience
31
00:01:47,720 --> 00:01:50,930
that we've added this
create or replace.
32
00:01:50,930 --> 00:01:54,080
We don't need to have
the or replace in here,
33
00:01:54,080 --> 00:01:58,070
but what it allows us to do is
recompile the object on the fly
34
00:01:58,070 --> 00:01:59,900
without dropping it first.
35
00:01:59,900 --> 00:02:03,340
So it's really just a
matter of convenience.
36
00:02:03,340 --> 00:02:05,650
So this procedure
does the same thing
37
00:02:05,650 --> 00:02:09,160
that our anonymous block
was programmed to do.
38
00:02:09,160 --> 00:02:11,710
So let's extend it
just a little bit.
39
00:02:11,710 --> 00:02:16,360
Let's use some of the
advantages of procedures
40
00:02:16,360 --> 00:02:19,600
in order to change this
to do a little more work.
41
00:02:19,600 --> 00:02:22,390
What we can do in a procedure
that's easier, really,
42
00:02:22,390 --> 00:02:25,240
than an anonymous
block is pass values
43
00:02:25,240 --> 00:02:27,400
into the procedure itself.
44
00:02:27,400 --> 00:02:34,460
So here, we're going to create
a variable called p exclude emp.
45
00:02:34,460 --> 00:02:37,260
It's an IN variable.
46
00:02:37,260 --> 00:02:38,140
And it is a varchar2.
47
00:02:40,730 --> 00:02:44,330
So p exclude emp is going
to be a variable that
48
00:02:44,330 --> 00:02:48,500
allows us to pass in a value to
the employee report procedure
49
00:02:48,500 --> 00:02:51,170
that will exclude an employee.
50
00:02:51,170 --> 00:02:53,330
We say that it's an IN
variable, because it
51
00:02:53,330 --> 00:02:55,640
comes into the
procedure as opposed
52
00:02:55,640 --> 00:02:58,490
to an out that
leaves the procedure.
53
00:02:58,490 --> 00:03:03,920
And it is a varchar2 data type,
meaning a character data type.
54
00:03:03,920 --> 00:03:11,270
So now I'm going to modify my
cursor to use this parameter
55
00:03:11,270 --> 00:03:12,590
value that we pass in.
56
00:03:17,780 --> 00:03:22,020
Now we need to recompile it,
because we've changed it.
57
00:03:22,020 --> 00:03:24,690
Compile successfully.
58
00:03:24,690 --> 00:03:26,790
And just to give you
an example of what
59
00:03:26,790 --> 00:03:31,370
would happen if
we made a mistake,
60
00:03:31,370 --> 00:03:34,700
notice that the compiler
log comes up and gives us
61
00:03:34,700 --> 00:03:38,450
an indication that there
is a problem with the code.
62
00:03:38,450 --> 00:03:40,210
Then we go out and debug it.
63
00:03:40,210 --> 00:03:43,940
And these problems and
compiler log information
64
00:03:43,940 --> 00:03:45,860
isn't always readily
available, and it
65
00:03:45,860 --> 00:03:49,070
takes some practice being able
to read these and find out
66
00:03:49,070 --> 00:03:50,360
what's wrong.
67
00:03:50,360 --> 00:03:52,400
We know what's wrong,
because we did it.
68
00:04:00,650 --> 00:04:03,530
And now it compiles
successfully.
69
00:04:03,530 --> 00:04:05,600
So all we've done at this
point is actually just
70
00:04:05,600 --> 00:04:06,980
create the code.
71
00:04:06,980 --> 00:04:08,930
We haven't done anything
that would actually
72
00:04:08,930 --> 00:04:10,710
execute the code.
73
00:04:10,710 --> 00:04:12,080
So before we do
this again, we're
74
00:04:12,080 --> 00:04:15,620
going to make sure that's
set server output on
75
00:04:15,620 --> 00:04:19,310
has been run, because we want
output to come to the screen.
76
00:04:19,310 --> 00:04:23,570
And we need to execute
this procedure.
77
00:04:23,570 --> 00:04:29,130
We use the exec command,
the name of the procedure,
78
00:04:29,130 --> 00:04:34,440
and then we pass in a
value to the procedure.
79
00:04:34,440 --> 00:04:38,460
So we're going to pass the
value Alan into the procedure.
80
00:04:38,460 --> 00:04:43,660
When it's executed, Alan gets
passed into this variable.
81
00:04:43,660 --> 00:04:45,490
It is accessed here.
82
00:04:45,490 --> 00:04:46,990
And so this will
be the same thing
83
00:04:46,990 --> 00:04:49,180
as saying select the
columns from emp,
84
00:04:49,180 --> 00:04:51,700
where e name not equal Alan.
85
00:04:54,470 --> 00:04:58,500
So let's execute this.
86
00:04:58,500 --> 00:05:02,540
So it comes through and uses
every row in the emp table
87
00:05:02,540 --> 00:05:07,370
to output this string
of variables and strings
88
00:05:07,370 --> 00:05:09,030
concatenated together.
89
00:05:09,030 --> 00:05:13,180
But notice that Alan
is excluded, because we
90
00:05:13,180 --> 00:05:15,620
passed in that value.
91
00:05:15,620 --> 00:05:18,940
So this, essentially,
is a simple example
92
00:05:18,940 --> 00:05:22,900
of how a procedure, or what we
might call a stored procedure,
93
00:05:22,900 --> 00:05:25,050
works in PL/SQL.
7430
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.