Professional Documents
Culture Documents
Training Guide
PL/SQL
for
Beginners
Supplement
Training Guide www.appltop.com
info@appltop.com
PL/SQL for Beginners - Supplement
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 didn’t have
time for".
Topics Covered
The following topics are covered:-
• PL/SQL Records & Tables
• Host File Access with UTL_FILE
l_record1 t_record;
l_record2 t_record;
BEGIN
l_record1.col1 := 10;
l_record1.col2 := 'Test';
l_record2 := l_record1;
END;
print_dept(l_rec);
END;
names_tab t_names;
BEGIN
names_tab(-10) := 'Fred';
names_tab(0) := 'John';
names_tab(250) := 'Richard';
END;
names_tab t_names;
BEGIN
names_tab(0) := 'Fred';
names_tab(1) := 'John';
names_tab(2) := 'Richard';
DBMS_OUTPUT.put_line(names_tab(3));
END;
number_tab t_table;
l_rows NUMBER;
BEGIN
number_tab(1) := 10;
number_tab(10) := 20;
number_tab(87) := 5;
number_tab(2500) := 100;
l_rows := number_tab.COUNT;
END;
number_tab t_table;
BEGIN
number_tab(1) := 10;
number_tab(10) := 20;
number_tab(87) := 5;
number_tab(2500) := 100;
number_tab.DELETE(87);
END;
number_tab t_table;
BEGIN
number_tab(1) := 10;
number_tab(10) := 20;
number_tab(87) := 5;
number_tab(2500) := 100;
IF number_tab.EXISTS(10) THEN
DBMS_OUTPUT.put_line('Row 10 Found');
END IF;
END;
number_tab t_table;
l_first BINARY_INTEGER;
l_last BINARY_INTEGER;
BEGIN
number_tab(1) := 10;
number_tab(10) := 20;
number_tab(87) := 5;
number_tab(2500) := 100;
l_first := number_tab.FIRST;
l_last := number_tab.LAST;
END;
number_tab t_table;
l_index BINARY_INTEGER;
BEGIN
number_tab(1) := 10;
number_tab(10) := 20;
number_tab(87) := 5;
number_tab(2500) := 100;
END;
UTL_FILE.fclose(l_handle);
or
UTL_FILE.fclose_all;
END;
Example
DECLARE
l_handle UTL_FILE.file_type;
BEGIN
l_handle := UTL_FILE.fopen( '/u01/app'
, 'my_file.dat'
, 'w');
UTL_FILE.fclose(l_handle);
END;
UTL_FILE.putf(l_handle,'arg1=%s\n','Fred');
UTL_FILE.fclose(l_handle);
END;
Example
DECLARE
l_handle UTL_FILE.file_type;
l_buffer VARCHAR2(500);
BEGIN
l_handle := UTL_FILE.fopen( '/u01/app'
, 'my_file.dat'
, 'r');
UTL_FILE.get_line(l_handle,l_buffer);
UTL_FILE.fclose(l_handle);
END;
l_handle UTL_FILE.file_type;
l_path VARCHAR2(50) := '/u01/app';
l_filename 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);
UTL_FILE.fflush(l_handle);
UTL_FILE.fclose(l_handle);
EXCEPTION
WHEN UTL_FILE.invalid_path THEN
DBMS_OUTPUT.put_line('Invalid Path');