溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

oracle 使用 UTL_FILE 導(dǎo)出表數(shù)據(jù)到txt文件

發(fā)布時間:2020-06-25 02:19:25 來源:網(wǎng)絡(luò) 閱讀:1750 作者:wangluochongzi 欄目:關(guān)系型數(shù)據(jù)庫

環(huán)境:

Red Hat Enterprise Linux Server release 6.6

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production



--創(chuàng)建目錄

create or replace directory TXT_DIR as '/opt/oracle/database/exptxt';

grant read,write on directory TXT_DIR to manager_desk;


--編寫存儲過程,并存入文件/home/oracle/detail.sql

create or replace PROCEDURE appdetail

is

file_handle utl_file.file_type;

Write_content VARCHAR2(5000);

v_DETAILID        NUMBER;

v_APPID           NUMBER;

v_APPVER          NUMBER;

v_APPVERNAME      VARCHAR2(254);

v_PACKAGENAME     VARCHAR2(254);

v_CRC32           VARCHAR2(20);

v_ICON            VARCHAR2(254);

v_DESCRIPTION     CLOB;

v_CSIZE           NUMBER;

v_PAYTYPE         NUMBER;

v_COST            NUMBER(25,3);

v_STATE           NUMBER;

v_CREATORID       NUMBER;

v_MODDATE         DATE;

v_APPPATH         VARCHAR2(500);

v_ISDEF           NUMBER;

v_DOWNLOADCOUNT   NUMBER;

v_COOPERATIONID   NUMBER;

v_REALAPPID       NUMBER(38);

v_CREATEDATE      DATE;

v_ISADVERTISER    NUMBER(38);

v_EDITOR          NUMBER;

v_ISGETDATA       NUMBER;

v_SECURITYSTATUS  NUMBER;

v_ADTYPES         NUMBER;

v_PERMISSIONLEVEL NUMBER;

v_VERIFIED        NUMBER;

v_BOTTOMDESC      VARCHAR2(512);

v_SIGNATUREMD5    VARCHAR2(64);

v_APKMD5          VARCHAR2(64);

v_MINVERSIONCODE  NUMBER(38);

v_PCLASSID        NUMBER(38);

v_CLASSID         NUMBER(38);

cursor cur_sp_out

is

select detailid, appid, appver, appvername, packagename, crc32, icon, description, csize, paytype, cost, state, creatorid, moddate, apppath, isdef, downloadcount, cooperationid, realappid, createdate, isadvertiser, editor, isgetdata, securitystatus, adtypes, permissionlevel, verified, bottomdesc, signaturemd5, apkmd5, minversioncode, pclassid, classid from applljkll;

begin

open cur_sp_out;

loop

    fetch cur_sp_out into v_detailid, v_appid, v_appver, v_appvername, v_packagename, v_crc32, v_icon, v_description, v_csize, v_paytype, v_cost, v_state, v_creatorid, v_moddate, v_apppath, v_isdef, v_downloadcount, v_cooperationid, v_realappid, v_createdate, v_isadvertiser, v_editor, v_isgetdata, v_securitystatus, v_adtypes, v_permissionlevel, v_verified, v_bottomdesc, v_signaturemd5, v_apkmd5, v_minversioncode, v_pclassid, v_classid;

    exit when cur_sp_out%notfound;

    file_handle := utl_file.fopen('TXT_DIR','applljkll.txt','a',32000);

    --specified  directories ,file_name and max_linesize 

write_content := v_detailid||', '||v_appid||', '||v_appver||', '||v_appvername||', '||v_packagename||', '||v_crc32||', '||v_icon||', '||v_description||', '||v_csize||', '||v_paytype||', '||v_cost||', '||v_state||', '||v_creatorid||', '||v_moddate||', '||v_apppath||', '||v_isdef||', '||v_downloadcount||', '||v_cooperationid||', '||v_realappid||', '||v_createdate||', '||v_isadvertiser||', '||v_editor||', '||v_isgetdata||', '||v_securitystatus||', '||v_adtypes||', '||v_permissionlevel||', '||v_verified||', '||v_bottomdesc||', '||v_signaturemd5||', '||v_apkmd5||', '||v_minversioncode||', '||v_pclassid||', '||v_classid;

    --write file

    IF utl_file.is_open(file_handle) THEN

     utl_file.put_line(file_handle,write_content);

    END IF;

    --close file

    utl_file.fclose(file_handle);

    end loop;

    close cur_sp_out;

end;

/


執(zhí)行:

SQL>  @/home/oracle/base.sql


Procedure created.



SQL> exec appdetail;


PL/SQL procedure successfully completed.


處錯:

SQL> exec appbase

BEGIN appbase; END;


*

ERROR at line 1:

ORA-29285: file write error

ORA-06512: at "SYS.UTL_FILE", line 77

ORA-06512: at "SYS.UTL_FILE", line 690

ORA-06512: at "MANAGER_DESK.APPBASE", line 55

ORA-06512: at line 1

解決:

修改

max_linesize 為 3200;

file_handle := utl_file.fopen('TXT_DIR','qn_desktop_appdetail.txt','a',32000);

注:如果不知道m(xù)ax_linesize,其默認值為1024;


utl_file 子過程 fopen語法:


Syntax

UTL_FILE.FOPEN (
   location     IN VARCHAR2,
   filename     IN VARCHAR2,
   open_mode    IN VARCHAR2,
   max_linesize IN BINARY_INTEGER) 
  RETURN file_type;




向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI