Linux/unix passing variables between shell SQL

Source: Internet
Author: User
Tags sqlplus

Flexible combination of Linux/unix shell and SQL variable transfer, greatly improve the efficiency of the DBA, this article for Linux/unix Shell SQL passed between the variables given a few simple examples for reference.

Linux/unix cut with Sql,ramn please refer to: Sql,rman script in Linux/unix shell script

One, example

[Python]View PlainCopyprint?
  1. 1, the shell variable accepts the SQL return value the way one
  2. [Email protected]:~> more./retval.sh
  3. #!/bin/bash
  4. Retval= ' Sqlplus-silent Scott/tiger <<eof
  5. SET PAGESIZE 0 FEEDBACK off VERIFY off HEADING off ECHO off
  6. SELECT * from emp WHERE ename=' SCOTT ';
  7. EXIT;
  8. EOF '
  9. If [-Z "$RETVAL"]; Then
  10. echo "No rows returned from database"
  11. Exit 0
  12. Else
  13. Echo $RETVAL
  14. Fi
  15. [Email protected]:~> chmod u+x retval.sh
  16. [Email protected]:~>./retval.sh
  17. 7788 SCOTT ANALYST 7566 19-apr- 34171.88
  18. 2, the shell variable accepts the SQL return value the way two
  19. [Email protected]:~> more./retval_2.sh
  20. #!/bin/bash
  21. # Author:robinson Cheng
  22. # blog:http://blog.csdn.net/robinson_0612
  23. Sqlplus-s "Scott/tiger" <<eof
  24. SET PAGESIZE 0 FEEDBACK off VERIFY off HEADING off ECHO off
  25. Col CNT New_value v_cnt
  26. SELECT COUNT (*) CNT from emp WHERE deptno=10;
  27. Exit v_cnt
  28. Eof
  29. Value="$?"
  30. echo "Show rows for Deptno: $VALUE"
  31. 3. Pass the shell variable as a parameter to the SQL script
  32. [Email protected]:~> more./retval_3.sh
  33. #!/bin/bash
  34. v_empno="$"
  35. Sqlplus-s "Scott/tiger" <<eof
  36. SET PAGESIZE 0 FEEDBACK off VERIFY off HEADING off ECHO off
  37. SELECT ename from emp WHERE empno= $v _empno;
  38. Exit
  39. Eof
  40. Exit
  41. [Email protected]:~>./retval_3.sh 7788
  42. SCOTT
  43. Ext.: http://blog.csdn.net/leshami/article/details/7978179

Linux/unix passing variables between shell SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.