在Sqlplus中运行Linux函数

时间:2016-03-15 20:17:07

标签: linux oracle sqlplus

我可以使用HOST命令从Oracle SQLPLUS内部调用用户创建的shell函数吗?如果没有,那么解决问题的最佳方法是什么?

基本上,我想运行一个shell文件:

Shell commands
sqlplus
@file.sql
HOST mylinuxfunction...
@file2.sql
HOST anotherlinuxfunction..
exit
Shell commands

谢谢!

1 个答案:

答案 0 :(得分:0)

你肯定可以从SQLPlus脚本中调用HOST命令,但我想你真的要求是否可以在其余的SQLPlus脚本中使用linux函数的返回值。您也可能希望在Linux函数中使用SQL查询的结果。

如果你需要将SQL信息传递给你的linux函数,并且需要在SQL的其余部分中访问linux函数的结果,那么你所拥有的几乎会起作用。这会

date
sqlplus / << xxENDxx \
    @file.sql
    HOST mylinuxfunction...
    @file2.sql
    HOST anotherlinuxfunction..
    exit
xxENDxx
date

现在,如果你想从你的linux函数获取信息到SQL,你将不得不使用外部表;很多设置,但请看这里:https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:439619916584并搜索&#34;但这是另一种有趣的方法,可在10.2.0.5和更高版本中使用:&#34;

如果要将在调用SQLPlus之前完成的Linux命令中的信息传递到SQL命令中,就像这样,将行插入到uptimes表中,并使用uptime命令输出存储在BASH变量$UPTIMES中:

#!/bin/bash
if [ "$1" = "" ]
then
    echo Missing User ID parm
    exit 1
else
    USER_ID=$1
fi

read  -p "Enter Your password for Oracle instance $ORACLE_SID for user $USER_ID: " PW

UPTIMES=`uptime | awk -F,  '{print $3, $4, $5}' | awk '{printf "%2.2f,%2.2f,%2.2f\n", $3, $4, $5}'`
sqlplus /nolog << xxENDxx \

connect $USER_ID/$PW

        insert into uptimes (date_stamp, one_min, five_min, fifteen_min) values (sysdate, $UPTIMES);
        HOST ls -o uptimes.sh
        --@file2.sql
        select * from uptimes;
        HOST du -sh .
        exit
xxENDxx

date

调用上面的内容给出了这个:

oracle. (/home/oracle/sql)
Linux> ./uptimes.sh mark.stewart
Enter Your password for Oracle instance ecs03 for user mark.stewart: xxxx

SQL*Plus: Release 12.1.0.2.0 Production on Thu Mar 17 20:09:36 2016
Dev:@> Connected.
Dev:MARK.STEWART@ecs03> Dev:MARK.STEWART@ecs03>
1 row created.

Dev:MARK.STEWART@ecs03> -rwxr-xr-x. 1 oracle 548 Mar 17 20:09 uptimes.sh

Dev:MARK.STEWART@ecs03> Dev:MARK.STEWART@ecs03>
DATE_STAM    ONE_MIN   FIVE_MIN FIFTEEN_MIN
--------- ---------- ---------- -----------
17-MAR-16          0        .01         .05
17-MAR-16          0        .01         .05
17-MAR-16          0        .01         .05

Dev:MARK.STEWART@ecs03>  146M    .

Dev:MARK.STEWART@ecs03> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Thu Mar 17 20:09:36 CET 2016
oracle. (/home/oracle/sql)
Linux>