Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,440 --> 00:00:05,730
In this lesson, we're going to
be looking at PL/SQL functions.
2
00:00:05,730 --> 00:00:09,870
So Oracle includes certain
functions within its database
3
00:00:09,870 --> 00:00:13,920
product by default, things
like single-row functions,
4
00:00:13,920 --> 00:00:17,700
such as upper or
lower or substring,
5
00:00:17,700 --> 00:00:23,100
and multi-row functions, such
as average or min or max.
6
00:00:23,100 --> 00:00:26,610
When we program a PL/SQL
function, what we're actually
7
00:00:26,610 --> 00:00:28,980
doing is creating our own.
8
00:00:28,980 --> 00:00:32,490
So rather than using an
Oracle-supplied function that
9
00:00:32,490 --> 00:00:34,620
may not do the job
that we want, we're
10
00:00:34,620 --> 00:00:38,620
creating our own function
to operate in the same way.
11
00:00:38,620 --> 00:00:41,500
So to begin, let's
just take a quick look
12
00:00:41,500 --> 00:00:44,200
at some of those
supplied functions.
13
00:00:47,670 --> 00:00:51,620
So in this case, the emp
table has the ename column.
14
00:00:51,620 --> 00:00:53,940
All of those values
are in uppercase.
15
00:00:53,940 --> 00:00:57,800
But we're passing in each
row value in the column
16
00:00:57,800 --> 00:00:59,750
into the lower function.
17
00:00:59,750 --> 00:01:01,730
And it returns in lowercase.
18
00:01:05,920 --> 00:01:09,300
So the sal column, for
salary, in the emp table
19
00:01:09,300 --> 00:01:13,260
has values for salary and
has many of them-- actually,
20
00:01:13,260 --> 00:01:16,210
14 of them-- in this
particular table.
21
00:01:16,210 --> 00:01:19,470
And when we use the
average function,
22
00:01:19,470 --> 00:01:22,650
we're passing those
values into the function.
23
00:01:22,650 --> 00:01:24,000
And it comes up with an average.
24
00:01:24,000 --> 00:01:26,490
It computes an average
for all of the values
25
00:01:26,490 --> 00:01:28,060
that are passed to it.
26
00:01:28,060 --> 00:01:30,810
So when we create
a PL/SQL function,
27
00:01:30,810 --> 00:01:33,180
we're doing essentially
the same thing,
28
00:01:33,180 --> 00:01:36,570
only we're writing the
code behind the function.
29
00:01:36,570 --> 00:01:38,190
So let's take a look
at how this works.
30
00:01:40,810 --> 00:01:44,070
We're going to find that
the structure of a function
31
00:01:44,070 --> 00:01:48,400
is much like the
structure of a procedure.
32
00:01:48,400 --> 00:01:53,130
So we create a replace
function, give it a name.
33
00:01:53,130 --> 00:01:55,340
We're going to have
a parameter variable
34
00:01:55,340 --> 00:01:59,900
that we pass in called ename.
35
00:01:59,900 --> 00:02:05,030
However, we do have one thing
that's distinct in this case.
36
00:02:05,030 --> 00:02:08,270
We use the statement
RETURN number
37
00:02:08,270 --> 00:02:11,000
to indicate the data
type of the value
38
00:02:11,000 --> 00:02:14,390
that we're going to return
out of the function.
39
00:02:14,390 --> 00:02:18,680
What makes a function distinct
from PL/SQL, like a procedure,
40
00:02:18,680 --> 00:02:22,550
is that it's actually going to
return a value to the calling
41
00:02:22,550 --> 00:02:24,630
statement.
42
00:02:24,630 --> 00:02:28,550
Next, we have our
declarative section.
43
00:02:28,550 --> 00:02:33,140
So we declare our
local variables
44
00:02:33,140 --> 00:02:35,980
and begin our execution.
45
00:02:35,980 --> 00:02:37,030
We're going to select.
46
00:02:48,920 --> 00:02:52,190
So let's look at our
processing section here.
47
00:02:52,190 --> 00:02:54,740
What we're doing is we're
using a select statement
48
00:02:54,740 --> 00:02:56,430
within our processing.
49
00:02:56,430 --> 00:03:00,560
However, we're using it in a
PL/SQL context, if you will,
50
00:03:00,560 --> 00:03:07,880
because we're selecting the
column sal plus sal times 0.10.
51
00:03:07,880 --> 00:03:12,240
And we're selecting it
into one of our variables,
52
00:03:12,240 --> 00:03:17,300
the lv_new_sal from
emp where the employee
53
00:03:17,300 --> 00:03:22,160
name, ename, equals p_ename,
which is our parameter that we
54
00:03:22,160 --> 00:03:23,180
pass in.
55
00:03:23,180 --> 00:03:26,360
So we're going to pass in
the name of an employee.
56
00:03:26,360 --> 00:03:30,260
And where that row is matched,
it's going to select the salary
57
00:03:30,260 --> 00:03:33,530
plus salary times 0.10--
58
00:03:33,530 --> 00:03:36,350
so in essence, a 10% raise--
59
00:03:36,350 --> 00:03:39,380
hence the name of the
function, give_raise.
60
00:03:39,380 --> 00:03:41,600
So now we'll compile it.
61
00:03:41,600 --> 00:03:43,630
It compiled correctly.
62
00:03:43,630 --> 00:03:48,020
And let's take a look at
how we access this function.
63
00:03:48,020 --> 00:03:50,740
When we just did the
examples of functions--
64
00:03:50,740 --> 00:03:52,930
single-row functions,
multi-row functions,
65
00:03:52,930 --> 00:03:55,780
like lower and
average-- we used them
66
00:03:55,780 --> 00:03:57,980
in the context of
a select statement.
67
00:03:57,980 --> 00:04:00,890
And we do the same
in this case as well.
68
00:04:00,890 --> 00:04:04,120
So we say select
give_raise, which
69
00:04:04,120 --> 00:04:06,250
is the name of our function.
70
00:04:06,250 --> 00:04:10,090
We pass in a value from dual.
71
00:04:10,090 --> 00:04:11,950
So what does dual mean?
72
00:04:11,950 --> 00:04:16,030
Well, dual in Oracle is
what we call a pseudo table.
73
00:04:16,030 --> 00:04:18,010
So it's not really a table.
74
00:04:18,010 --> 00:04:20,770
But it's actually a way
to process statements
75
00:04:20,770 --> 00:04:22,270
like functions.
76
00:04:22,270 --> 00:04:24,130
It's used in many context.
77
00:04:24,130 --> 00:04:28,060
It's sort of a Swiss army
knife to be used as a table.
78
00:04:28,060 --> 00:04:30,760
So when you need to
do a select statement
79
00:04:30,760 --> 00:04:33,460
and you don't have a real
table to select against,
80
00:04:33,460 --> 00:04:35,170
you use the dual table.
81
00:04:35,170 --> 00:04:37,660
So if we select
sysdate from dual,
82
00:04:37,660 --> 00:04:41,770
it comes back with the current
date of the server's time,
83
00:04:41,770 --> 00:04:42,880
if you will.
84
00:04:42,880 --> 00:04:46,240
So here, we're using
give_raise just in the same way
85
00:04:46,240 --> 00:04:49,870
that we did the average
function or the lower function,
86
00:04:49,870 --> 00:04:54,250
passing in the value ALLEN,
which comes to p_ename.
87
00:04:54,250 --> 00:04:59,080
We do this query, where ename
equals the value we passed in,
88
00:04:59,080 --> 00:05:01,270
which was ALLEN.
89
00:05:01,270 --> 00:05:05,210
We select that into
the lv_new_sal variable
90
00:05:05,210 --> 00:05:06,850
that we declared here.
91
00:05:06,850 --> 00:05:09,850
And then we return that
to the calling statement,
92
00:05:09,850 --> 00:05:11,180
which is here.
93
00:05:11,180 --> 00:05:14,250
So let's see how it works.
94
00:05:14,250 --> 00:05:23,080
And to compare this, let's see
what his salary was before--
95
00:05:23,080 --> 00:05:25,290
1,600.
96
00:05:25,290 --> 00:05:27,570
And so when we used
the give_raise function
97
00:05:27,570 --> 00:05:31,260
to calculate what his
new salary would be,
98
00:05:31,260 --> 00:05:34,110
it returns the value of 1,760.
99
00:05:34,110 --> 00:05:38,260
So this is a PL/SQL
function at work.
7843
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.