shell腳本oracle資料庫
A. linux下編寫以個shell腳本,實現對oracle資料庫的查詢結果保存在一個變數中
empno=100不存在的,改成有的數據了。
#!/bin/bash
result=$(sqlplus -s 'scott/tiger@dbname'<<EOF
spool test.txt
set pages 0
set feed off
set heading off;
set feedback off;
set verify off;
set linesize 1000;
SELECT * FROM scott.emp where empno=7369;
spool off
EOF
)
echo $result
~
~
~
~
~
~
~
~
~
"test.sh" 14L, 256C written
oracle@****:~> ./test.sh
7369 SMITH CLERK 7902 17-DEC-80 2240.06 20
oracle@****:~> more test.txt
7369 SMITH CLERK 7902 17-DEC-80 2240.06
20
B. shell腳本語言如何和oracle結合在一起使用
如果是通過shell腳本運行Oracle的sql語句,可以這樣寫shell腳本:
echo 「Oracle SQL Example"
sqlplus / as sysdba <<EOF
select * from scott.emp;
EOF
也就是把sql語句寫到shell腳本的EOF之間。
C. 如何用shell腳本將在mysql資料庫中得到的數據導入到oracle資料庫中
有一個工具是mysql到oracle做數據遷移的叫Convert Mysql to Oracle 你可以試試,不知道合不合適。
非要弄shell的話,那可真是麻煩可以選擇讓程序員寫個小程序轉換sql的讓後用shell調用。
真自己寫shell。。。那就折騰導出來的 create、insert語句吧。想想都頭大。是在沒必要完全用shell弄。
D. 怎麼樣用shell做一個連接oracle資料庫的腳本
shell中直接調用sqlplus即可
sqlplus -s 用戶名/口令@實例名<<EOF
E. 寫個shell腳本連接oracle資料庫查詢某表數據導出為txt文件,再發送到第三
1、簡單的單列
#!/bin/sh
sqlplus 'user001/12345678'<< EOF
set define off
set hea off
spool vip1.txt
select username from ACCOUNT where LEVEL=7;
spool off
quit;
EOF
sed -i 's/[ ]*//g' ~/vip1.txt
sed -i '/^$/d' ~/vip1.txt
sed -i '1d' ~/vip1.txt
sed -i '$d' ~/vip1.txt
scp -P22 ~/vip1.txt [email protected]:/root
2、復雜的多列
#!/bin/sh
cid=$1;
today=`date +%Y-%m-%d-%H.%M`
ym=`date +%Y%m`
ymd=`date -d -1days +%Y%m%d`
last_ym=`date -d last-month +%Y%m`
next_ym=`date -d next-month +%Y%m`
file=chat_recorder_${cid}_20140707-11.xls
if [[ $1 == '' ]];then
echo "Usage: $0 company_id "
exit 0;
fi
sqlplus 'user002/12345678' << EOF
set linesize 200
set term off verify off feedback off pagesize 999
set markup html on entmap ON spool on preformat off
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';
spool ${file}
select a.*,b.* from recorder_${ym} a,t_${ym} b where a.company_id='$cid' and a.create_time between TO_DATE('2014-07-07 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and TO_DATE('2014-07-12 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and a.chat_id=b.chat_id order by b.chat_id ;
spool off
quit;
EOF
sed -i '/select/d' $file
zip -r ${file}.zip $file
scp -P22 ${file}.zip [email protected]:/opt
F. 求一份shell腳本,需求是:從Oracle資料庫中提取一個表中的數據輸出到文件,並且每條記錄一行;
應用spool命令,大量數據匯出很方便,腳本內容大致如下:
--============================================
#!/bin/sh
#第一步
sqlplus -s 用戶名/密碼@服務名<<EOF
spool customers.sql --輸出文件路徑及名稱
set trimspool on
set linesize 8000
set pagesize 50000
set newpage 1
set heading off
set term off
set feedback off
set sqlblankline off
SELECT A || '|+|' || --欄位A
B || '|+|' || --欄位B
C || '|+|' || --欄位C
D || '|+|' || --欄位D
E || '|+|' || --欄位E
F || '|+|' || --欄位F
G || '|+|' || --欄位G
TO_CHAR(H,'YYYYMMDD HH:MM:SS') || '|+|' --欄位H,可以使用函數
FROM CUSTOMERS;--表名
spool off
exit
EOF
--=======================
其中'|+|'為分隔符,也可以換成你說的逗號,即','。調用該腳本後,在根目錄下生成customers.sql文件。
G. 怎樣在shell腳本中嵌入Oracle資料庫操作
在shell裡面執行sqlplus,大致如下
sqlplus username/password@sid << EOF >> xxxx.log
select field_name from table_name where ....;
exit;
EOF
然後從輸出log裡面分析出你要的值
