oracle UNIX 内部/内部 IF..THEN 语句中的 SQL 选择语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/282222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:27:29  来源:igfitidea点击:

SQL select statement in UNIX inside/within IF..THEN statement

oracleunixshellsqlplus

提问by ConcernedOfTunbridgeWells

I just want ask the steps when trying to create a simple SQL select statement in UNIX inside/within IF..THEN..FI statement.

我只是想在 UNIX 内部/内部 IF..THEN..FI 语句中尝试创建简单的 SQL select 语句时询问步骤。

I know how to use the 'select' and 'if..then' statements in SQL*Plus, but I'm having a difficulties having a UNIX script to point to variables: If 'ABC' to 'Select...'

我知道如何在 SQL*Plus 中使用 'select' 和 'if..then' 语句,但是我在使用 UNIX 脚本指向变量时遇到了困难:If 'ABC' to 'Select...'

Example:

例子:

if [ "$?" = 'ABC' ] then SELECT employid, name, age FROM tablename; else exit 1 fi

如果 [ "$?" = 'ABC'] 然后从表名中选择员工 ID、姓名、年龄;否则退出 1 fi

if [ "$?" = 'XYZ' ] then SELECT employid, name, age FROM tablename; else exit 1 fi

如果 [ "$?" = 'XYZ'] 然后从表名中选择员工 ID、姓名、年龄;否则退出 1 fi

How do I put it in a UNIX script more correctly syntax wise and right to the point?

我如何将它放在 UNIX 脚本中更正确地语法明智和切中要害?

Thanks.

谢谢。

回答by ConcernedOfTunbridgeWells

This sounds like you're trying to embed SQLPlus in a shell script. From memory the incantation should look something like:

这听起来像是在尝试将 SQLPlus 嵌入到 shell 脚本中。根据记忆,咒语应该是这样的:

if [ $? -eq ABC ]; then
SQLPLUS /S USER/PASS@Instance <<EOF
SET echo off;
SET pagesize 0;
SET heading off;
SPOOL foo.out
select foo from bar
EOF
fi

Everything between the SQLPLUS and EOF is passed to SQLPlus, so we have some statements to control the formatting (you may want different ones) and the actual query. The SPOOL command in the SQLPlus script sends the output to a file. For more detailed docs on using SQLPlus, You can download them from Oracle's web site.

SQLPLUS 和 EOF 之间的所有内容都传递给 SQLPlus,因此我们有一些语句来控制格式(您可能需要不同的格式)和实际查询。SQLPlus 脚本中的 SPOOL 命令将输出发送到文件。有关使用 SQLPlus 的更详细文档,您可以从Oracle 网站下载它们

回答by staticsan

Remember that echois your friend.

记住那echo是你的朋友。

if [ "$?" = "ABC" ] then echo SELECT employid, name, age FROM tablename; else exit 1; fi

You can embed shell script variables and such. Be careful of the need to quote things the shell wants to act upon, like quotes and semi-colons.

您可以嵌入 shell 脚本变量等。小心需要引用 shell 想要操作的东西,比如引号和分号。

回答by Gary Myers

Have you considered using perl or other scripting language that includes database connection functionality. That way you avoid the clunky shell script/SQL*Plus linkage

您是否考虑过使用 perl 或其他包含数据库连接功能的脚本语言。这样你就可以避免笨重的 shell 脚本/SQL*Plus 链接

回答by Gary Myers

The above answer was ok. However, I knew that, using SQLPlus in a shell script and unfortunately, I don't need the SQLPlus script to send the output to a file. In other words: Is there any other way of doing this, just print the output to a log?

上面的回答没问题。但是,我知道,在 shell 脚本中使用 SQLPlus,不幸的是,我不需要 SQLPlus 脚本将输出发送到文件。换句话说:有没有其他方法可以做到这一点,只需将输出打印到日志中?