linux - 在 Sqlplus 中运行 Linux 函数

标签 linux oracle sqlplus

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

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

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

谢谢!

最佳答案

您当然可以从 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并搜索“但这是另一种有趣的方法,在 10.2.0.5 及更高版本中可用:”

如果你想将在调用 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>

关于linux - 在 Sqlplus 中运行 Linux 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36021286/

相关文章:

oracle - 安装Oracle 11g后连接到SQL * Plus时应输入什么用户名和密码?

Oracle sqlplus 行大小

linux - sqlplus 中大于 4GB 的假脱机文件

linux - 并行脚本中的 Bash 重定向

java - 使用 Bootstrap 和 Tomcat Embedded 以及未运行的 Java SE 8 创建 Web 应用程序

sql - 如果根据oracle中的出生日期列年龄列为空,如何更新年龄列?

oracle - 更新 Oracle 包中的单个过程

linux - 删除带有模式的特殊字符的行

linux - 在 FT232RL 上使用 libftdi 后恢复虚拟 Com 端口

linux - 恢复插入到 postgres 数据库