Mac 編寫oracle 連線指令碼

Rocky_Ansi發表於2016-11-27

 

首先需要本地存有sqlplus命令, 如果沒有則需要到官網下載 也可點選我進行下載 (解壓 readme.txt 有安裝配置說明):

Oracle官網下載instant client for os x。

http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
至少要下載
instantclient-basic-macos和 instantclient-sqlplus-macos兩個包
解壓縮這兩個包到目錄instantclient中
複製instantclient到/opt/oracle目錄中
sudo cp -r instantclient /opt/oracle  (執行命令前先建立這資料夾)
二. 配置環境變數
1. 啟動終端Terminal
2. 進入當前使用者的home目錄    輸入cd ~
3. 建立.bash_profile    輸入touch .bash_profile
4. 編輯.bash_profile檔案    輸入vi  .bash_profile
環境變數配置
DYLD_LIBRARY_PATH="/opt/oracle/instantclient"
export DYLD_LIBRARY_PATH
export ORACLE_HOME=$DYLD_LIBRARY_PATH
export PATH=$ORACLE_HOME:$PATH

5. 儲存檔案,關閉.bash_profile
6. 更新剛配置的環境變數
    輸入source .bash_profile

這樣在命令列中即可直接使用sqlplus,   修改字符集, 可直接寫在sh指令碼中
export NLS_LANG="simplified chinese_china.zhs16gbk"

 

呼叫示例

function get_access_reference()
{
reference=`sqlplus -S  dbname/dbpwd@tns << EOF
set feedback off;
set pagesize 0;
select ATL_REFERENCE from t_access_tradelist where atl_primary = ${1};
EOF`
}
echo ${reference} # 直接輸出查詢結果

 

 

相關文章