Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,370 --> 00:00:05,690
In this lesson, we're going to
look at the concept of a packet
2
00:00:05,690 --> 00:00:07,520
PL/SQL package.
3
00:00:07,520 --> 00:00:13,310
So a package is a way to take
different units of PL/SQL code
4
00:00:13,310 --> 00:00:16,250
and bring them together
into a single unit--
5
00:00:16,250 --> 00:00:18,540
a single package, if you will.
6
00:00:18,540 --> 00:00:21,650
And this facilitates
the sharing of code.
7
00:00:21,650 --> 00:00:25,860
And makes it easier to access
different pieces of code.
8
00:00:25,860 --> 00:00:30,250
So in the same way that
having lots of anonymous block
9
00:00:30,250 --> 00:00:33,500
PL/SQL scripts out
on a file system
10
00:00:33,500 --> 00:00:38,060
is more cumbersome than using
procedures and functions--
11
00:00:38,060 --> 00:00:41,060
those kinds of code that
are stored in the database--
12
00:00:41,060 --> 00:00:43,340
packaging takes it
one step further.
13
00:00:43,340 --> 00:00:45,320
And allows us
rather than to have
14
00:00:45,320 --> 00:00:47,240
lots of different
procedures that
15
00:00:47,240 --> 00:00:49,790
have to be accessed separately
or different functions
16
00:00:49,790 --> 00:00:53,120
or combinations, we get to
bring them all together.
17
00:00:53,120 --> 00:00:55,040
So when code is related--
18
00:00:55,040 --> 00:00:58,490
these different pieces of code-
we can bring them together
19
00:00:58,490 --> 00:01:00,110
into a package.
20
00:01:00,110 --> 00:01:04,010
Now a package is composed of two
different units-- if you will--
21
00:01:04,010 --> 00:01:08,100
a package specification
and a package body.
22
00:01:08,100 --> 00:01:10,850
So let's define a
package specification.
23
00:01:16,170 --> 00:01:21,270
So what I'm working with here
are two different sets of code.
24
00:01:21,270 --> 00:01:22,640
So we have a function here--
25
00:01:22,640 --> 00:01:25,140
the give raise function.
26
00:01:25,140 --> 00:01:28,560
And then we have the
employee report procedure.
27
00:01:28,560 --> 00:01:31,770
So what we do in a package
is we define each one
28
00:01:31,770 --> 00:01:33,900
of the units of our code--
29
00:01:33,900 --> 00:01:36,870
the procedure and the function.
30
00:01:36,870 --> 00:01:40,200
And we define those in
the package specification.
31
00:01:40,200 --> 00:01:42,240
And that's what we're
designating when we
32
00:01:42,240 --> 00:01:45,470
say create or replace package.
33
00:01:45,470 --> 00:01:51,200
So our employee report
procedure, our definition
34
00:01:51,200 --> 00:01:52,970
of in parameter variables.
35
00:02:03,620 --> 00:02:06,590
And remember that SQL
and PL/SQL are not case
36
00:02:06,590 --> 00:02:10,770
sensitive unless the
strings are within quotes.
37
00:02:10,770 --> 00:02:14,210
So we can use uppercase
RETURN, lower case RETURN.
38
00:02:14,210 --> 00:02:16,640
Our create or replace can
be upper or lowercase--
39
00:02:16,640 --> 00:02:17,630
it doesn't matter.
40
00:02:17,630 --> 00:02:20,840
It usually just comes
down to coding standards.
41
00:02:20,840 --> 00:02:24,320
So here, we have a package that
contains a procedure called
42
00:02:24,320 --> 00:02:30,470
employee report, that takes this
input as a VARCHAR2 data type
43
00:02:30,470 --> 00:02:32,840
and it has this
function-- give raise--
44
00:02:32,840 --> 00:02:39,090
that takes this variable in as
a VARCHAR2 and returns a number.
45
00:02:39,090 --> 00:02:41,100
So let's compile this.
46
00:02:41,100 --> 00:02:42,530
So our package is compiled.
47
00:02:42,530 --> 00:02:45,140
So now we have
the specification,
48
00:02:45,140 --> 00:02:47,900
but these procedures
in the packages
49
00:02:47,900 --> 00:02:50,040
don't actually do anything.
50
00:02:50,040 --> 00:02:51,550
So we need to create
a package body.
51
00:02:55,920 --> 00:02:59,940
And here-- to save time from
rewriting a lot of code--
52
00:02:59,940 --> 00:03:02,240
I'm going to go
grab the procedure--
53
00:03:02,240 --> 00:03:06,120
and I'm just going to start at
the word procedure, All the way
54
00:03:06,120 --> 00:03:07,750
to the end.
55
00:03:07,750 --> 00:03:11,510
But I want to specify
the name of the procedure
56
00:03:11,510 --> 00:03:13,460
at the end statement--
57
00:03:13,460 --> 00:03:17,830
just employee report.
58
00:03:17,830 --> 00:03:19,660
So here's the package body.
59
00:03:19,660 --> 00:03:23,050
And it has the definition of
the procedure that's referenced
60
00:03:23,050 --> 00:03:26,440
in the package specification.
61
00:03:26,440 --> 00:03:28,920
Now we'll grab function.
62
00:03:32,650 --> 00:03:38,400
And now an end to the
entire package itself.
63
00:03:38,400 --> 00:03:42,480
all the way to the beginning of
our package body and Compile,
64
00:03:42,480 --> 00:03:44,100
to see if we got it.
65
00:03:44,100 --> 00:03:46,930
And it looks like we did.
66
00:03:46,930 --> 00:03:50,320
So now we have our package
specification and our package
67
00:03:50,320 --> 00:03:51,270
body.
68
00:03:51,270 --> 00:03:55,150
And we can actually see this
over here in SQL Developer.
69
00:03:55,150 --> 00:03:58,510
Now that we've created
them, we see an EMP package.
70
00:03:58,510 --> 00:04:04,100
We expand that, it shows we have
an Employee Report and a Give
71
00:04:04,100 --> 00:04:05,030
Raise.
72
00:04:05,030 --> 00:04:07,460
And then it has the body.
73
00:04:07,460 --> 00:04:09,280
And for each one of these.
74
00:04:09,280 --> 00:04:12,200
And we just click
on it and here,
75
00:04:12,200 --> 00:04:15,530
shows us the procedure
within the package body.
76
00:04:19,750 --> 00:04:22,630
In order to access or
run these packages,
77
00:04:22,630 --> 00:04:26,510
we need to reference our
command with the name.
78
00:04:26,510 --> 00:04:32,350
So for instance-- in running
the give raise procedure,
79
00:04:32,350 --> 00:04:36,080
we would simply use it
in a SELECT statement.
80
00:04:36,080 --> 00:04:40,400
But because the give raise
function belongs in a package,
81
00:04:40,400 --> 00:04:44,540
we need to do a different
type of call to the package.
82
00:04:44,540 --> 00:04:52,020
So we say select
EMPpackage.giveraise,
83
00:04:52,020 --> 00:04:55,820
pass in the value
from the dual table.
84
00:05:03,260 --> 00:05:06,730
Notice that it says the
package is in an invalid state.
85
00:05:06,730 --> 00:05:13,060
So when this occurs, we'll go
all the way to the beginning,
86
00:05:13,060 --> 00:05:17,580
make sure we've got everything,
and recompile the package.
87
00:05:17,580 --> 00:05:20,820
Notice that it says, we cannot
compile the package without its
88
00:05:20,820 --> 00:05:22,500
specification.
89
00:05:22,500 --> 00:05:26,710
So let's go and do that, first.
90
00:05:26,710 --> 00:05:30,000
Compiles successfully.
91
00:05:30,000 --> 00:05:33,830
Package compiles successfully.
92
00:05:33,830 --> 00:05:37,220
And now the EMP package
give raise returns
93
00:05:37,220 --> 00:05:40,240
the value of 1,760.
94
00:05:40,240 --> 00:05:43,090
So why did we have the
problems with this?
95
00:05:43,090 --> 00:05:48,820
It's because anytime we change
a package body, in any way,
96
00:05:48,820 --> 00:05:51,610
we need to also compile
it specification
97
00:05:51,610 --> 00:05:54,860
if there's changes in there
that have affected it.
98
00:05:54,860 --> 00:05:56,980
So we always have
to keep that in mind
99
00:05:56,980 --> 00:05:59,380
whenever we're
compiling packages.
100
00:05:59,380 --> 00:06:02,470
It's best to compile them
together at the same time.
101
00:06:02,470 --> 00:06:08,280
So using packages can make the
storage and retrieval of code--
102
00:06:08,280 --> 00:06:12,180
PL/SQL code-- much simpler,
and more straightforward,
103
00:06:12,180 --> 00:06:14,810
and a little more
organized, as well.
8299
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.