Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2831413
  • 博文数量: 599
  • 博客积分: 16398
  • 博客等级: 上将
  • 技术积分: 6875
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-30 12:04
个人简介

WINDOWS下的程序员出身,偶尔也写一些linux平台下小程序, 后转行数据库行业,专注于ORACLE和DB2的运维和优化。 同时也是ios移动开发者。欢迎志同道合的朋友一起研究技术。 数据库技术交流群:58308065,23618606

文章分类

全部博文(599)

文章存档

2014年(12)

2013年(56)

2012年(199)

2011年(105)

2010年(128)

2009年(99)

分类: Oracle

2012-08-19 14:18:59

有网友问到SHELL如何接受存储过程返回的值,做了一个简单的小例子:
 

点击(此处)折叠或打开

  1. [oracle@rac1 ~]$ sqlplus htyansp/htyansp

  2. SQL*Plus: Release 11.2.0.1.0 Production on Sun Aug 19 13:05:19 2012

  3. Copyright (c) 1982, 2009, Oracle. All rights reserved.


  4. Connected to:
  5. Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
  6. With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
  7. Data Mining and Real Application Testing options

  8. SQL> CREATE OR REPLACE PROCEDURE GETDATE(p_date OUT VARCHAR2)
  9.   2 IS
  10.   3 BEGIN
  11.   4 p_date:=TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS');
  12.   5 END;
  13.   6 /

  14. Procedure created.

  15. SQL> VARIABLE str VARCHAR2(20);
  16. SQL> exec GETDATE(:str);

  17. PL/SQL procedure successfully completed.

  18. SQL> select :str from dual;

  19. :STR
  20. --------------------------------

  21. 2012-08-19 13:08:31

首先创建了一个存储过程,返回当前的日期,可以看到存储过程已经正常工作。
编写的SHELL脚本如下:
 

点击(此处)折叠或打开

  1. [oracle@rac1 ~]$ cat getdate.sh
  2. #!/bin/bash

  3. function getdate(){
  4. $ORACLE_HOME/bin/sqlplus -s htyansp/htyansp <<EOF
  5. set term off
  6. set feedback off
  7. set head off
  8. VARIABLE str VARCHAR2(20);
  9. exec GETDATE(:str);
  10. select 'date|'||:str from dual;
  11. exit;
  12. EOF
  13. }

  14. mydate=`getdate|grep "date"| awk -F "|" '{print $2}'`
  15. echo $mydate

  16. [oracle@rac1 ~]$ chmod u+x getdate.sh
  17. [oracle@rac1 ~]$ ./getdate.sh
  18. 2012-08-19 13:23:14

如果问题复杂了,用SHELL出来起来麻烦,用C来处理或许更好。
 

点击(此处)折叠或打开

  1. [oracle@rac1 ~]$ cat getdate.pc
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. exec sql include sqlca;

  7. void sqlerror()
  8.   {
  9.     exec sql whenever sqlerror continue;
  10.     printf("\n% .70s \n", sqlca.sqlerrm.sqlerrmc);
  11.     exec sql rollback work release;
  12.     exit(1);
  13.   }

  14. int main(int argc,char *argv)
  15. {
  16.   exec sql begin declare section;
  17.     char str[20];
  18.     char * connstr="yansp/yansp";
  19.   exec sql end declare section;

  20.   exec sql whenever sqlerror do sqlerror();
  21.   exec sql connect :connstr;
  22.   exec sql call getdate(:str);
  23.   printf("The date is :%s\n",str);
  24.   exec sql commit work release;
  25.   exit(0);
  26. }

  27. [oracle@rac1 ~]$ proc parse=none getdate.pc

  28. Pro*C/C++: Release 11.2.0.1.0 - Production on Sun Aug 19 14:14:30 2012

  29. Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.

  30. System default option values taken from: /home/oracle/app/product/11.2.0/db_1/precomp/admin/pcscfg.cfg

  31. [oracle@rac1 ~]$ gcc getdate.c -o getdate $ORACLE_HOME/lib/libclntsh.so
  32. [oracle@rac1 ~]$ ./getdate
  33. The date is :2012-08-19 14:14:34

阅读(3188) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~