PLSQL 操作 ORACLE 伺服器上的檔案

2021-09-06 09:33:28 字數 3115 閱讀 2841

在做oracle plsql

程式設計的時候,難免要對檔案進行操作,如讀取

oracle

伺服器上的別的目錄下的資料檔案,匯入到

oracle

庫中;雖然在

plsql developer

工具中提供了

debug

功能,但當有輸出時,

debug

的控制台不能及時看到,除非停止

debug

。如果在

plsql

程式中輸出日誌資訊到檔案中,使得

plsql

程式除錯、執行跟中更加方便,因為大家都知道日誌的作用是什麼。

下面講講怎麼在

oracle

的plsql

程式中寫入日誌到

oracle

伺服器的檔案中。雖然我們是用

plsql developer

開發和執行

plsql

程式,但是

plsql developer

經常是安裝在遠端計算機上,這裡說的操作檔案,是指

plsql

操作oracle dbms

所在的伺服器上,如果

oracle

安裝在linux

系統,則是操作

linux

上的檔案;安裝在

windows

上,則是操作

windows

上的檔案,對於本次演示前者會相對麻煩些,後者容易些。因為我用的

oracle

安裝在linux

上,所以以

linux

系統為例講解。

注:「--」

表示注釋行

一、建立存放日誌檔案的目錄。

1.以root

許可權登入

linux

系統。 2

.建立乙個目錄存放日誌檔案。

# mkdir

/home

/oracle

/plog

3.進入

/home

/oracle

/,賦予

oracle

使用者對plog

目錄讀寫許可權

# chown oracle plog

4.檢驗

oracle

使用者是否對該目錄有讀寫許可權,切換到

oracle

使用者下

# su oracle $ cd plog $ touck plsql

.log

-- 這裡是建立乙個檔案的意思,如果沒有許可權,則建立失敗並提示。

-- 如果建立成功,則說明許可權授權成功,我們可以把

plsql.log

刪除$ rm plsql

.log

,這個隨便。

二、介紹

plsql

程式中操作檔案,

oracle

提供utl_file

包是專門用來操作磁碟上的檔案。

1.需要建立乙個

directory物件

sql>

create

or replace

directory

plog_dir

as'/home/oracle/plog';

2.以sys

使用者登入

plsql developer

,給執行

plsql

的程式使用者授權對該目錄可寫、可讀的許可權;對

utl_file

有可執行的許可權。

sql>

grant

read,write

on directory

plog_dir

toscott;

sql>

grant

execute

onutl_file

toscott

;--scott

是登入oracle

資料庫的乙個使用者。3.

編寫乙個寫入日誌的儲存過程。

create

orreplace

procedure

logger

( v_file_name

invarchar2,--

存放日誌檔名

v_log_msg

invarchar2

--日誌訊息)

is v_file_handle utl_file

.file_type;--

宣告乙個檔案操作控制代碼

begin

v_file_handle

:=utl_file

.fopen

('plog_dir'

,v_file_name

,'a'

);--

例項化乙個控制代碼

utl_file

.put_line

(v_file_handle

,v_log_msg

);--

寫入資訊

utl_file

.fflush

(v_file_handle

);--

把緩衝區的資訊寫入檔案

utl_file

.fclose

(v_file_handle

);--

關閉檔案控制代碼

exception

when

others

then

dbms_output.put_line('

插入日誌異常,錯誤**是:

'||sqlcode||

'錯誤訊息是:

'||sqlerrm);

end;

注:1.

'plog_dir'

是directory

,必須大寫2.

'a'是在日誌檔案後追加資訊

三、在需要寫入日誌的地方呼叫該儲存過程,就可以了。

execute

logger

('wt.txt'

,'this is a testing for plsql writer !');

PLSQL操作ORACLE伺服器上的檔案

quote size medium 在做oracle plsql 程式設計的時候,難免要對檔案進行操作,如讀取oracle伺服器上的別的目錄下的資料檔案,匯入到oracle庫中 雖然在plsql developer工具中提供了debug功能,但當有輸出時,debug的控制台不能及時看到,除非停止de...

pl sql連線遠端oracle伺服器

前提是電腦必須安裝oracle客戶端解壓版。配置方法 1 找到tnsnames.ora檔案。2 用文字方式開啟,新增以下內容 1 2 3 4 5 6 7 8 本地例項名 description address protocol tcp host 遠端資料庫ip位址 port 遠端伺服器端口號 con...

PLSQL檢視Oracle的sql操作記錄

1 在plsql按鍵ctrl e 可以查詢我們在plsql執行過的歷史sql,包括時間 使用者 語句 2 select from v sql 執行sql查詢,查詢內容包含所有使用者和應用系統對資料庫的操作,執行過的sql 3 select from v process 查詢資料庫的程序 4 v se...