Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,200 --> 00:00:02,570
In this lesson,
we're going to be
2
00:00:02,570 --> 00:00:06,770
taking a look at an
anonymous block of PL/SQL.
3
00:00:06,770 --> 00:00:10,220
So this is going to be
the PL/SQL programming
4
00:00:10,220 --> 00:00:14,570
language, specifically in the
form of an anonymous block.
5
00:00:14,570 --> 00:00:18,260
Now, the anonymous block we
could think of as a script.
6
00:00:18,260 --> 00:00:20,960
So this is a PL/SQL script.
7
00:00:20,960 --> 00:00:24,260
It's not a compiled
object, but it is a script,
8
00:00:24,260 --> 00:00:28,190
and it would live in a file
outside the database somewhere,
9
00:00:28,190 --> 00:00:31,290
and then could be run
against the database.
10
00:00:31,290 --> 00:00:36,520
So let's connect up and
type in the basic framework
11
00:00:36,520 --> 00:00:37,630
of an anonymous block.
12
00:00:45,020 --> 00:00:48,670
So this is the basic format
of an anonymous block
13
00:00:48,670 --> 00:00:50,710
PL/SQL script.
14
00:00:50,710 --> 00:00:53,510
So we begin with
the DECLARE section.
15
00:00:53,510 --> 00:00:54,970
And the DECLARE
section is where we
16
00:00:54,970 --> 00:00:59,110
have variable declaration,
data types and values,
17
00:00:59,110 --> 00:01:01,060
and any definitions
of any cursors--
18
00:01:01,060 --> 00:01:04,150
and we'll discuss what
those are in a moment.
19
00:01:04,150 --> 00:01:06,510
The next is the BEGIN block.
20
00:01:06,510 --> 00:01:08,610
This block will have the
executable statements
21
00:01:08,610 --> 00:01:10,440
that actually do the work.
22
00:01:10,440 --> 00:01:12,090
And then we have
an END statement
23
00:01:12,090 --> 00:01:13,590
that puts an end to it.
24
00:01:13,590 --> 00:01:16,700
So our declarations
will be in this area,
25
00:01:16,700 --> 00:01:20,540
and our statements
will be in this area.
26
00:01:20,540 --> 00:01:23,000
Before we go too
much further, it's
27
00:01:23,000 --> 00:01:29,850
usually a good habit to
type serveroutput on--
28
00:01:29,850 --> 00:01:33,000
and we'll highlight
that and execute it.
29
00:01:33,000 --> 00:01:36,990
This will allow us to get the
output to come to the screen.
30
00:01:36,990 --> 00:01:40,920
PL/SQL is built for doing
work against database data,
31
00:01:40,920 --> 00:01:44,100
and so by default doesn't
output things to the screen.
32
00:01:44,100 --> 00:01:46,110
It's not as interactive as that.
33
00:01:46,110 --> 00:01:49,740
But if we set serveroutput
on that tells it to output
34
00:01:49,740 --> 00:01:52,440
visually the output
to the screen.
35
00:01:52,440 --> 00:01:56,010
So what we're going to do is
modify our basic structure
36
00:01:56,010 --> 00:01:59,230
to do a little bit of work.
37
00:01:59,230 --> 00:02:00,930
So I'm going to
declare some variables.
38
00:02:07,040 --> 00:02:08,670
So these are our
variable declarations.
39
00:02:08,670 --> 00:02:11,520
We have a variable
called lv_name, lv_job,
40
00:02:11,520 --> 00:02:16,560
and lv_hiredate, and the lv is
a standard for local variable.
41
00:02:16,560 --> 00:02:19,890
You can name them job, name,
and hiredate if you like.
42
00:02:19,890 --> 00:02:21,580
This is the data type.
43
00:02:21,580 --> 00:02:24,540
So they're character
data, max width of 20,
44
00:02:24,540 --> 00:02:27,270
and lv_hiredate is a date.
45
00:02:27,270 --> 00:02:28,650
Next we'll define a cursor.
46
00:02:35,220 --> 00:02:37,000
So this is a cursor definition.
47
00:02:37,000 --> 00:02:38,390
Now, what a cursor is--
48
00:02:38,390 --> 00:02:41,420
it's an object that we set
up, basically encapsulates
49
00:02:41,420 --> 00:02:42,840
a SELECT statement.
50
00:02:42,840 --> 00:02:46,760
So when we create a cursor,
we define it as being,
51
00:02:46,760 --> 00:02:50,690
in this case, select these three
columns from the emp table.
52
00:02:50,690 --> 00:02:53,360
PL/SQL has constructs
like this to enable
53
00:02:53,360 --> 00:02:56,510
us to more easily work
with database data.
54
00:02:56,510 --> 00:02:58,850
You won't see these
types of constructs
55
00:02:58,850 --> 00:03:02,000
in other third-generation
languages like C.
56
00:03:02,000 --> 00:03:03,680
They have to be done
more explicitly.
57
00:03:03,680 --> 00:03:06,590
But because PL/SQL is
designed to work with data,
58
00:03:06,590 --> 00:03:10,490
we get to use
constructs like this.
59
00:03:10,490 --> 00:03:12,670
So now we're in
our BEGIN section,
60
00:03:12,670 --> 00:03:15,430
where we have our
actual statements.
61
00:03:15,430 --> 00:03:19,920
So we're going to
open our cursor.
62
00:03:19,920 --> 00:03:22,980
We're going to set up a loop.
63
00:03:22,980 --> 00:03:26,970
Now, this statement will
bring back more than one row,
64
00:03:26,970 --> 00:03:29,760
and we want to do
something to each row
65
00:03:29,760 --> 00:03:31,710
as it comes into the loop.
66
00:03:31,710 --> 00:03:33,390
So we set up a loop.
67
00:03:33,390 --> 00:03:37,740
We cursor through these
rows, operate on each one,
68
00:03:37,740 --> 00:03:40,540
and then exit when we come
to the end of the table.
69
00:03:40,540 --> 00:03:43,710
So that's what our looping
constructs are all about.
70
00:03:43,710 --> 00:03:52,500
We say fetch the cursor
into our local variables.
71
00:03:52,500 --> 00:03:55,270
So notice, here, that
we're fetching our cursor--
72
00:03:55,270 --> 00:03:58,490
which is this SELECT statement--
into these variables--
73
00:03:58,490 --> 00:04:02,380
ename into lv_name,
job into lv_job,
74
00:04:02,380 --> 00:04:04,990
and hiredate into lv_hiredate.
75
00:04:04,990 --> 00:04:09,700
And we type exit when
the CURSOR%NOTFOUND--
76
00:04:12,480 --> 00:04:15,840
and again, the code
is not case-sensitive.
77
00:04:15,840 --> 00:04:21,600
The %NOTFOUND is a keyword to
signal the exit of the cursor
78
00:04:21,600 --> 00:04:24,900
when the condition occurs
that there's no more data
79
00:04:24,900 --> 00:04:26,850
in the cursor itself.
80
00:04:26,850 --> 00:04:28,560
So now we're looping.
81
00:04:28,560 --> 00:04:31,320
We're telling it to exit
whenever we don't find any more
82
00:04:31,320 --> 00:04:32,890
data in the cursor.
83
00:04:32,890 --> 00:04:36,650
So now let's do the work that
we intended to do with it.
84
00:04:36,650 --> 00:04:39,870
dbms_output is an
Oracle-supplied package
85
00:04:39,870 --> 00:04:42,920
that basically prints data.
86
00:04:42,920 --> 00:04:46,900
So here I have a string literal
for employee, double bar that
87
00:04:46,900 --> 00:04:49,030
symbolizes
concatenation-- so we're
88
00:04:49,030 --> 00:04:53,940
going to concatenate this
string to a variable.
89
00:05:02,060 --> 00:05:03,980
All right, so what we're
doing in this case is
90
00:05:03,980 --> 00:05:06,560
we're looping
through this cursor,
91
00:05:06,560 --> 00:05:09,710
and each time a
new row comes in,
92
00:05:09,710 --> 00:05:12,920
it has new values for
lv_name, job, and hiredate,
93
00:05:12,920 --> 00:05:15,780
because they're fetched
into the active cursor.
94
00:05:15,780 --> 00:05:20,310
And then we're using
dbms_output_line to just write
95
00:05:20,310 --> 00:05:23,540
these strings concatenated
with the variable.
96
00:05:23,540 --> 00:05:25,560
But because of the
nature of the strings
97
00:05:25,560 --> 00:05:27,960
we put in here and the
data that it goes with,
98
00:05:27,960 --> 00:05:31,450
we can see some logic
that comes from this.
99
00:05:31,450 --> 00:05:37,100
So now we need to end the loop,
and then end the statement.
100
00:05:37,100 --> 00:05:40,590
So server output is on.
101
00:05:40,590 --> 00:05:44,940
Let's highlight it
and attempt to run it.
102
00:05:44,940 --> 00:05:47,570
So notice we get the
notification anonymous block
103
00:05:47,570 --> 00:05:49,780
completed.
104
00:05:49,780 --> 00:05:51,370
See this a little better.
105
00:05:51,370 --> 00:05:56,260
And so it fetches the cursor
and goes through each row of emp
106
00:05:56,260 --> 00:05:59,410
and gets ename,
job, and hire date,
107
00:05:59,410 --> 00:06:01,390
and it concats it
with the string.
108
00:06:01,390 --> 00:06:04,760
So we get some sort of
useful information from this.
109
00:06:04,760 --> 00:06:07,090
So based on the information
in the emp table,
110
00:06:07,090 --> 00:06:10,030
we could say employee
Smith works as a clerk
111
00:06:10,030 --> 00:06:12,100
and was hired on this date.
112
00:06:12,100 --> 00:06:14,780
Employee Allen works as
a salesman-- et cetera,
113
00:06:14,780 --> 00:06:15,710
et cetera.
114
00:06:15,710 --> 00:06:18,610
And it does it 14 times
to go through every row
115
00:06:18,610 --> 00:06:20,730
in the ename database.
116
00:06:20,730 --> 00:06:24,190
And so that's the basic
structure and operation
117
00:06:24,190 --> 00:06:27,000
of an anonymous block PL/SQL.
9372
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.