database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
ㆍOracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
Oracle Q&A 3254 게시물 읽기
No. 3254
Re: PL/SQL PROCDDURE 예제 구합니당!!!(파일입출력이 있는)
작성자
아마도
작성일
2001-03-06 20:05
조회수
3,995

먼저 utl_file_dir 파라메터가 설정되어 있는지 확인해

보아야 합니다.

 

SELECT name, value, isses_modifiable, issys_modifiable

FROM v$parameter

WHERE name = 'utl_file_dir';

 

위에서 설정된 디렉토리에만 I/O가 가능합니다.

 

다음은 어느 책인가에 나와있는 2가지 예제입니다.

 

DECLARE

lv_file_id_num UTL_FILE.FILE_TYPE;

BEGIN

lv_file_id_num := UTL_FILE.FOPEN('c:\apps\loaders',

'test4.dat', 'W');

UTL_FILE.FCLOSE(lv_file_id_num);

END;

/

 

 

 

 

 

CREATE OR REPLACE PROCEDURE process_products

(p_directory_txt VARCHAR2 := 'c:\apps\loaders',

p_filename_txt VARCHAR2 := 'prodlist.dat',

p_string_txt VARCHAR2) IS

-- DESCRIPTION: This package processes a product data file by

-- loading all product records into the s_product table. The

-- file loaded is scanned while loaded to log each line that

-- a specified string was found on. The statistics of the load

-- are also logged.

lv_file_id_num UTL_FILE.FILE_TYPE;

lv_file_id_num_2 UTL_FILE.FILE_TYPE;

lv_output_filename_txt VARCHAR2(30);

lv_filename_txt VARCHAR2(30);

lv_error_desc_txt VARCHAR2(50);

lv_line_cnt_num PLS_INTEGER := 0;

lv_error_cnt_num PLS_INTEGER := 0;

lv_buffer_txt VARCHAR2(2000);

lv_found_bln BOOLEAN := FALSE;

 

-- This procedure reduces the code redundancy in the exception

-- handling process and is called by each exception condition.

PROCEDURE error_processing (p_file_num UTL_FILE.FILE_TYPE,

p_line_num PLS_INTEGER, p_error_txt VARCHAR2) IS

BEGIN

ROLLBACK;

UTL_FILE.PUT_LINE(p_file_num, 'Line: ' ||

p_line_num || ' Error: ' || p_error_txt);

UTL_FILE.PUT_LINE(p_file_num, '----------------------------');

UTL_FILE.PUT_LINE(p_file_num, 'File Process ABORTED');

UTL_FILE.FCLOSE_ALL;

END error_processing;

BEGIN

-- Opens the input data file on the server for reading.

lv_file_id_num := UTL_FILE.FOPEN(p_directory_txt,

p_filename_txt, 'R');

-- Creates the log file name by stripping the file

-- extension and adding .log on the end.

lv_output_filename_txt := SUBSTR(p_filename_txt, 1,

INSTR(p_filename_txt, '.') - 1) || '.log';

-- Opens the log file on the server for writing.

lv_file_id_num_2 := UTL_FILE.FOPEN(p_directory_txt,

lv_output_filename_txt, 'W');

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'Processing Products Log File');

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'----------------------------');

UTL_FILE.PUT_LINE(lv_file_id_num_2, 'Directory: ' ||

p_directory_txt);

UTL_FILE.PUT_LINE(lv_file_id_num_2, 'Input File: ' ||

p_filename_txt);

UTL_FILE.PUT_LINE(lv_file_id_num_2, 'Output File: ' ||

lv_output_filename_txt);

UTL_FILE.PUT_LINE(lv_file_id_num_2, 'Search String: ' ||

p_string_txt);

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'----------------------------');

LOOP

lv_buffer_txt := NULL;

-- When end of file reached, the loop is terminated.

BEGIN <<read_file>>

UTL_FILE.GET_LINE(lv_file_id_num, lv_buffer_txt);

EXCEPTION

WHEN NO_DATA_FOUND THEN

EXIT;

END read_file;

lv_line_cnt_num := lv_line_cnt_num + 1;

-- If an error encountered on the insert, then the line

-- and error are logged to the log file and processing

-- continues.

BEGIN <<insert_product>>

-- When processing fixed length data files, if the spaces

-- are not trimmed off the left and right side, the spaces

-- will be part of the value inserted.

INSERT INTO s_product

(product_id, product_name, short_desc)

VALUES

(RTRIM(LTRIM(SUBSTR(lv_buffer_txt, 1, 7), ' '), ' '),

RTRIM(LTRIM(SUBSTR(lv_buffer_txt, 8, 50), ' '), ' '),

RTRIM(LTRIM(SUBSTR(lv_buffer_txt, 58), ' '), ' '));

EXCEPTION

WHEN OTHERS THEN

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'Line: ' || lv_line_cnt_num || ' Error: ' ||

SUBSTR(SQLERRM,1,200));

lv_error_cnt_num := lv_error_cnt_num + 1;

END insert_product;

-- The line number is written to the log file every time

-- the search string is found.

IF INSTR(UPPER(lv_buffer_txt), UPPER(p_string_txt)) > 0 THEN

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'String Found on Line: ' || lv_line_cnt_num);

lv_found_bln := TRUE;

END IF;

END LOOP;

COMMIT; -- If the search string is not found in the file, a message is

-- written to the log file to indicate this condition.

IF NOT lv_found_bln THEN

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'The string was not found in the file.');

END IF;

-- Final processing statistics are written to the log file.

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'----------------------------');

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'Number of Total Products Processed: ' ||

TO_CHAR(lv_line_cnt_num, '999,999'));

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'Number of Products Inserted: ' ||

TO_CHAR(lv_line_cnt_num - lv_error_cnt_num, '999,999'));

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'Number of Products with Error: ' ||

TO_CHAR(lv_error_cnt_num, '999,999'));

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'----------------------------');

UTL_FILE.PUT_LINE(lv_file_id_num_2,

'File Processed Successfully');

UTL_FILE.FCLOSE_ALL;

EXCEPTION

-- If any of the UTL_FILE exceptions are raised or any errors

-- encountered, the entire process is rolled back, and the line

-- number and error number are written to the log file.

WHEN UTL_FILE.internal_error THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.INTERNAL_ERROR encountered');

WHEN UTL_FILE.invalid_filehandle THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.INVALID_FILEHANDLE encountered');

WHEN UTL_FILE.invalid_mode THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.INVALID_MODE encountered');

WHEN UTL_FILE.invalid_operation THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.INVALID_OPERATION encountered');

WHEN UTL_FILE.invalid_path THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.INVALID_PATH encountered');

WHEN UTL_FILE.read_error THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.READ_ERROR encountered');

WHEN UTL_FILE.write_error THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

'UTL_FILE.WRITE_ERROR encountered');

WHEN OTHERS THEN

error_processing(lv_file_id_num_2, lv_line_cnt_num,

SUBSTR(SQLERRM,1,200));

END process_products;

/

 

 

 

 

>>sissky 님께서 쓰시길<<

 

:: PL/SQL 을 이용해서 프로시져를 짜야하거덩요...

:: 음...입력받는값은 없구요..걍 프로시져안에서..SQL문을 돌려서나온값을을 TXT파일로 출력되도

:: 록 하는 거예여...

:: oracle package 안에...파일입출력에 사용되는 function들은 있던데...

:: 어케 사용해야할지 잘몰라서요.

:: 혹시..예제를 가지구 계시면 좀 보여주세요.

:: 부탁드려요...^^

:: .

:: .

:: 참 그리구...혹시 프로시져가 서버에 있다가 매일매일 특정 시간이 되면 실행되게 하는법 알구

:: 계신분 있나요?그럼 꼭꼭 연락 주세요..

:: 꼭이요~~~~~

[Top]
No.
제목
작성자
작성일
조회
3218date 타입은 어떻게 처리하나요?
전병제
2001-03-01
1614
3253┕>Re: date 타입은 어떻게 처리하나요?
아마도
2001-03-06 19:51:18
1839
3213혹시 이런건 vb+oracle
이석환
2001-02-28
2001
3212원격으로 오라클 설치시 이런 에러 메시지가 발생합니다. 무엇때문인지....
김일권
2001-02-28
1552
3214┕>Re: 원격으로 오라클 설치시 이런 에러 메시지가 발생합니다. 무엇때문인지.... [1]
문태준
2001-03-01 12:10:22
2043
3226┕>Re: 원격으로 오라클 설치시 이런 에러 메시지가 발생합니다. 무엇때문인지....
savour
2001-03-02 14:54:06
2221
3210PL/SQL PROCDDURE 예제 구합니당!!!(파일입출력이 있는)
sissky
2001-02-28
2191
3254┕>Re: PL/SQL PROCDDURE 예제 구합니당!!!(파일입출력이 있는)
아마도
2001-03-06 20:05:04
3995
3206dbassist에서 2%를 넘기지 못하고 정지상태 ... 우에할꼬
foxyman
2001-02-28
1344
3207┕>Re: dbassist에서 2%를 넘기지 못하고 정지상태 ... 우에할꼬
savour
2001-02-28 15:55:07
1747
3209 ┕>Re: Re: dbassist에서 2%를 넘기지 못하고 정지상태 ... 우에할꼬
foxyman
2001-02-28 16:23:46
1595
3215  ┕>그거 원래 시간 오래 걸리는 것인데요?
문태준
2001-03-01 12:26:40
1650
3240   ┕>glibc를 2.1로 낮추어 설치 하셔야합니다....
이성재
2001-03-05 18:10:01
1952
3202oracle startup 시 에러..
이석환
2001-02-28
1437
3203┕>Re: oracle startup 시 에러..
소몰이
2001-02-28 12:13:06
1754
3204┕>Re: oracle startup 시 에러..
bobae2
2001-02-28 13:34:49
2019
31993185번 답변에 관하여.....
궁금이
2001-02-28
1812
3208┕>Re: 3185번 답변에 관하여.....
bobae2
2001-02-28 17:44:07
1738
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.031초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다