Professional Documents
Culture Documents
Appl Top 121704
Appl Top 121704
Training Guide
PL/SQL
for
Beginners
Supplement
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Supplement
The supplement covers a couple of topics that are
what you might call Advanced PL/SQL, the word
Advanced is purely relative, so a more appropriate
title may have been, "Some PL/SQL we didnt have
time for".
The PL/SQL for Beginners guide introduced you to
PL/SQL, the topics covered were based on what you
are most likely to come across in your early
developments.
I have picked the topics in this document based on
the fact that you are likely to use them from a
potential pot of hundreds of topics.
The style in which the topics are presented is
different to that of the training course. The main
course is in a tutorial style, whereas these topics are
much lighter in detail and by example.
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Topics Covered
The following topics are covered: PL/SQL Records & Tables
Host File Access with UTL_FILE
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Value
Fred
John
Richard
10
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
The above code will fail with the following error:ORA-01403: no data found
11
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Type Returned
NUMBER
DELETE
EXISTS
N/A
BOOLEAN
FIRST
BINARY_INTEGER
LAST
BINARY_INTEGER
NEXT
BINARY_INTEGER
PRIOR
BINARY_INTEGER
Description
12
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
13
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
table.DELETE(x);
remove row i.
table.DELETE(x,y);
14
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
15
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
16
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
17
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
18
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
19
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
20
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
PUT
PUT_LINE
NEW_LINE
PUTF
GET_LINE
21
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
PATH
FILENAME
MODE
IN VARCHAR2
IN VARCHAR2
IN VARCHAR2)
'/u01/app'
'my_file.dat'
'r');
22
Training Guide
www.appltop.com
info@appltop.com
'/u01/app'
'my_file.dat'
'r');
UTL_FILE.fclose(l_handle);
or
UTL_FILE.fclose_all;
END;
23
Training Guide
www.appltop.com
info@appltop.com
24
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
'/u01/app'
'my_file.dat'
'w');
25
Training Guide
www.appltop.com
info@appltop.com
'/u01/app'
'my_file.dat'
'w');
UTL_FILE.putf(l_handle,'arg1=%s\n','Fred');
UTL_FILE.fclose(l_handle);
END;
26
Training Guide
www.appltop.com
info@appltop.com
27
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
'/u01/app'
'my_file.dat'
'r');
UTL_FILE.get_line(l_handle,l_buffer);
UTL_FILE.fclose(l_handle);
END;
28
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
Raised When
Raised By
INVALID_PATH
INVALID_MODE
INVALID_FILEHANDLE
INVALID_OPERATION
FOPEN
FOPEN
FCLOSE,GET_LIN
E,PUT,PUTF,PUT_
LINE,NEW_LINE,
FFLUSH
GET_LINE,PUT,P
UTF,PUT_LINE,N
EW_LINE,FFLUSH
READ_ERROR
WRITE_ERROR
INTERNAL_ERROR
NO_DATA_FOUND
VALUE_ERROR
GET_LINE
FCLOSE,PUT,PUT
F,PUT_LINE,NEW
_LINE,FFLUSH,FC
LOSE_ALL
All
GET_LINE
GET_LINE
29
Training Guide
PL/SQL for Beginners - Supplement
www.appltop.com
info@appltop.com
UTL_FILE.file_type;
VARCHAR2(50) := '/u01/app';
VARCHAR2(20) := 'dept.dat';
l_record
VARCHAR2(200);
BEGIN
-- Open file
l_handle := UTL_FILE.fopen(
l_path
,
l_filename
,
'w');
-- Get all dept rows
FOR r_dept IN dept_cur
LOOP
l_record := TO_CHAR(r_dept.deptno)||'|'||
r_dept.dname||'|'||
TO_CHAR(r_dept.loc);
-- Write row to file
UTL_FILE.put_line(l_handle,l_record);
END LOOP;
UTL_FILE.fflush(l_handle);
UTL_FILE.fclose(l_handle);
EXCEPTION
WHEN UTL_FILE.invalid_path THEN
DBMS_OUTPUT.put_line('Invalid Path');
WHEN UTL_FILE.write_error THEN
DBMS_OUTPUT.put_line('Write Error');
END;
30