2009-12-18 123 views
1


我試圖將cat1.txt作爲變量($ 2),以便它將被插入到sql select語句中。有沒有辦法做到這一點?
從shell腳本傳遞變量到sql語句

my cat1.txt 
1111 
2334 
2234 
3333 
4444 
.... 
.... 
etc. 


我的SQL語句

set echo off linesize 280 pagesize 0 newpage 0 feedback 1 verify off head off trimspool on 
alter session set sort_area_size=104857600; 

define filename = &1 

spool &&filename 
prompt DATE TIME Service Name Amount 

select trim(to_char(trans_time,'yyyymmdd')) || chr(9) || 
    trim(to_char(trans_time,'hh24miss')) || chr(9) || 
    trim(prod_name)|| chr(9) || 
    (prod_price) 
where m_date = 11 and day >= 23 and day <= 30 
and (prod_bill) in ('1111','2334','2234','3333','4444') 
/
spool off 
exit 


我想試試這個,請參見下文。而不是手動鍵入prod_bill使其像文件名一樣變量。任何我應該添加到我的shell腳本?感謝:-)

from prod_trans 
where m_date = 11 and day >= 23 and day <= 30 
and (prod_bill) in ($2) 


我的shell腳本

#!/bin/sh 

export ORACLE_BASE=/u01/app/oracle 
export ORACLE_SID=prod 
export ORACLE_HOME=/u01/app/oracle/product/9.2.0 
export ORACLE_BASE ORACLE_HOME ORACLE_SID 
export PATH=$PATH:/usr/local/bin:$ORACLE_HOME/bin:/usr/sbin:/opt/java1.5/bin 
export USERNAME=prods 
export PASSWORD=prods 
export OUT_PATH=/home/prod/reports/adhoc/REp 
export SCRIPT=$OUT_PATH/BICScripts/Translog 
export OUTPUT=/home/prod/reports/adhoc/REp/Scripts/log/OUTPUT 
export FILENM=$OUTPUT/log_`date +%y%m%d`.txt 
sqlplus -s $USERNAME/[email protected]$ORACLE_SID @$SCRIPT/log.sql $FILENM 
+0

什麼版本的Oracle? – 2009-12-18 05:20:29

回答

0

我不知道是否有任何的方式來訪問從shell腳本Oracle數據庫,但是你可以使用例如Perl的?我想這將是很容易使用DBI來訪問數據庫。

當然,您可以使用shell腳本解析.sql文件,直接從cat1.txt添加prod_bills。

+0

謝謝,我已經使用Perl腳本:-) – Cez 2009-12-21 06:00:40

1
#!/bin/sh 
v_var=$1 
sqlplus -S system/manager << EOF`enter code here` 
SELECT username, account_status, expiry_date 
FROM dba_users WHERE lower(username)=lower('$v_var'); 
exit; 
EOF 

希望它解決了這個問題,我回答正確。