轉:sqlplus與shell互相傳值的幾種情況

來源:互聯網
上載者:User

標籤:

sqlplus與shell互相傳值的幾種情況

 

情況一:在shell中最簡單的調用sqlplus

$cat test.sh

#!/bin/sh

sqlplus oracle/[email protected]>file.log <<EOF

select * from test;

exit

EOF  #注意EOF要頂格寫

$sh test.sh

$cat file.log

--省略若干系統提示資訊-------

SQL>

EMPNO   EMPNAME          SAL      DEPTNO

-----   -------------    -----    ------

10002   Frank Naude      500      20

10001   Scott Tiger      1000     40

--省略若干系統提示資訊-------

將執行過程重新導向入檔案file.log,可通過cat file.log查看

 

情況二:直接將sqlplus的值賦值給shell變數

$cat test.sh

#!/bin/sh

# 將sqlplus的結果輸出給變數VALUE

# set命令的使用可查詢手冊

#注意shell中等號兩邊不能有空格

VALUE=`sqlplus -S /nolog <<EOF

set heading off feedback off pagesize 0 verify off echo off

conn oracle/[email protected]

select count(*) from test;

exit

EOF`

#輸出記錄數

echo "The number of rows is $VALUE."

$sh test.sh

The number of rows is    2.

顯示結果正確,表test共2條記錄

 

情況三:間接將sqlplus的值賦值給shell變數

$cat test.sh

#!/bin/sh

#利用COL column NEW_VALUE variable定義變數

#sqlplus執行完後最後傳回值為v_coun

#利用$?將最後傳回值賦值給VALUE,也即為test的記錄數

sqlplus -S /nolog <<EOF

set heading off feedback off pagesize 0 verify off echo off

conn oracle/[email protected]

col coun new_value v_coun

select count(*) coun from test;

exit v_coun

EOF

VALUE="$?"

echo "The number of rows is $VALUE."

$sh test.sh

         2

The number of rows is 2.

指令碼執行結果中第一個2為sqlplus傳回值,第二個2為VALUE的值

 

情況四:將shell變數的值傳給sqlplus使用

$cat test.sh

#!/bin/sh

#sqlplus引用shell變數TABLENAME的值

#注意賦值時,等號兩邊不能有空格

TABLENAME="test"

sqlplus -S oracle/[email protected] <<EOF

select * from ${TABLENAME};

exit

$sh test.sh

EMPNO EMPNAME                                                   SAL DEPTNO

----- -------------------------------------------------- ---------- ------

10002 Frank Naude                                               500 20

10001 Scott Tiger                                              1000 40

指令碼執行結果為:select * from test;的結果

 

情況五:通過互動方式手工輸入shell變數值

$cat test.sh

#!/bin/sh

#將手工輸入變數值讀入變數TABLENAME

echo  "Enter the tablename you want to select:"

read TABLENAME

sqlplus -S oracle/[email protected] <<EOF

select * from ${TABLENAME};

exit

$sh test.sh

#按提示輸入表名test

Enter the tablename you want to select:

test

 

EMPNO EMPNAME                                                   SAL DEPTNO

----- -------------------------------------------------- ---------- ------

10002 Frank Naude                                               500 20

10001 Scott Tiger                                              1000 40

指令碼執行結果為select * from test的執行結果

 

 

chapte 2:

 

有時因工作需要,得寫一些指令碼,都是shell和sqlplus混合的。

一般情況下,shell變數帶入到sql指令碼,比較方便,但是把sql的一些結果,輸出給shell,就比較麻煩一些了。以前用的方法比較土一點,就是在sqlplus裡面,spool到一個臨時檔案,然後在shell裡面用grep,awk一類的來分析這個輸出檔案。後來在網上看到一篇介紹,受益匪淺啊。在此表示感謝。

http://hi.baidu.com/edeed/blog/item/291698228a5694f4d7cae2c1.html/cmtid/e87926977f74636155fb968f

我試了三種,一個是退出sqlplus時順帶傳回值。這個方法有限制,只能是數字,不能是字串。

SQL> col global_name new_value xxx
SQL> select global_name from global_name;

GLOBAL_NAME
--------------------------------------------------------------------------------
XXX.XXX.XXX

SQL> exit xxx
SP2-0584: EXIT variable "XXX" was non-numeric

第二個就是直接select語句的輸出。

指令碼如下(test1.sh):

#!/bin/bash
VALUE=`sqlplus -S user/[email protected] <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
select * from global_name;
exit
EOF`

echo $VALUE

測試結果如下:

[[email protected] tmp]# sh test1.sh
XXX.XXX.COM
[[email protected] tmp]#

第三個是定義一個變數,然後在sqlplus裡面print。

指令碼如下(test2.sh):

#!/bin/bash

VALUE=`sqlplus -S user/[email protected]  <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
var username varchar2(30)
begin
  select user into :username from dual;
  :username := ‘username ‘|| :username;
end;
/

print username
exit
EOF`

echo $VALUE

測試結果如下:

[[email protected]]# sh test2.sh
username USER_A
[[email protected]]#

轉:sqlplus與shell互相傳值的幾種情況

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.