shell調用sqlplus查詢oracle

來源:互聯網
上載者:User

標籤:串連數   $1   today   time   file   最簡   bsp   nbsp   輸入   


[[email protected] shell_test]$ cat echo_time #!/bin/sh一.最簡單的調用sqlplussqlplus -S "sys/unimas as sysdba" << !select to_char(sysdate,‘yyyy-mm-dd‘) today from dual;exit;![[email protected] shell_test]$ ./echo_time

運行結果:

TODAY----------2011-03-21

-S 是silent mode,不輸出類似“SQL>”,串連資料庫,關閉資料庫之類的資訊。

EOF

eof可以是任何字串 比如"laldf"那麼當你輸入單獨一行laldf時"shell認為輸入結束,但是必須表示塊開始必須使用<<;
開始和結束要匹配這個符號“<<”後面的內容
舉例子:

 

[[email protected] shell_test]$ sqlplus -s "sys/unimas as sysdba" << abc> select to_char(sysdate,‘yyyy-mm-dd‘) today from dual;> exit;> abc

 

運行結果

TODAY----------2011-03-21
二.sqlplus的結果傳遞給shell的方法一

 

[[email protected] shell_test]$ cat test2.sh #!/bin/bashVALUE=`sqlplus -S "test/unimas" << !set heading offset feedback offset pagesize 0set verify offset echo offselect to_char(sysdate,‘yyyy-mm-dd‘) today from dual;exit!`echo $VALUEif [ -n "$VALUE" ]; thenecho "The rows is $VALUE"exit 0elseecho "There is no row"fi

 

三.sqlplus的結果傳遞給shell的方法二

 

[[email protected] shell_test]$ cat test1.sh #!/bin/bashsqlplus -S "test/unimas" << !set heading offset feedback offset pagesize 0set verify offset echo offcol coun new_value v_counselect count(*) coun from lesson;exit v_coun!VALUE="$?"echo "show row:$VALUE"

 

col coun new_value v_coun v_coun為number類型。因為exit 只能返回數實值型別。
四.把shell參數傳遞給sqlplu

 

#!/bin/basht_id="$1"sqlplus -S "test/unimas" << !set heading offset feedback offset pagesize 0set verify offset echo offselect teachername from teacher where id=$t_id;exit!

 

五.sqlplus的結果儲存在檔案中

 

#!/bin/shsqlplus -S "test/unimas"<<EOFset heading offset feedback offset pagesize 0set verify offset echo offspool spool_fileSELECT * from teacher;spool offexit;EOF

 

shell調用sqlplus查詢oracle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.