简体   繁体   English

将sqlplus值传递给shell变量

[英]pass sqlplus value to shell variable

the below picture shows what gets returned when I run sqlplus in shell 下图显示了我在shell中运行sqlplus时返回的内容

抽壳

but when I run this from the "run" command: 但是当我从“运行”命令运行它时:

powershell.exe -noexit c:\sqltriggers\voicetrigger2.ps1

with voicetrigger2.ps1 as this: 使用voicetrigger2.ps1如下:

$(sqlplus user/pass@OMP1 '@C:\sqltriggers\VOICEBLOCKTRIG.SQL');

I get this: 我明白了:

shell2

I should expect a 3 back. 我应该期待3回。 The issue is, I try to set that as a variable, and if the integer is greater than zero, run a BAT file. 问题是,我尝试将其设置为变量,如果整数大于零,则运行BAT文件。 But I don't think the SQLPlus is returning JUST an integer value. 但我不认为SQLPlus返回JUST一个整数值。 I think its actually returning this: 我认为它实际上归还了这个:

count(*)
      3

How do I get it to just return the integer value from the SQLplus command? 如何从SQLplus命令返回整数值?

SQL*Plus isn't returning anything, it's displaying the result of the query on standard output. SQL * Plus没有返回任何内容,它在标准输出上显示查询结果。 To get the direct call to only show 3 you can set heading off in the SQL script, and also call SQL*Plus with the -s flag to suppress the banner. 要直接调用show 3 ,可以在SQL脚本中set heading off ,也可以使用-s标志调用SQL * Plus来禁止set heading off You probably also want an exit at the end of the SQL script so it doesn't stay sitting at the SQL> prompt. 您可能还希望在SQL脚本的末尾exit ,以便它不会保持在SQL>提示符下。

The same applies to the powershell call, but there's something else going on there; 这同样适用于powershell调用,但那里还有其他东西; the 17 is a line number which means it's waiting for more input and hasn't executed the commands in the SQL script, which suggests either a query without a terminating ; 17是一个行号,这意味着它正在等待更多的输入并且没有执行SQL脚本中的命令,这表明查询没有终止; or / , or a PL/SQL block without a terminating / . / ,或没有终止/的PL / SQL块。 But if it's exactly the same SQL you ran in the first example then that is a bit odd as they should behave the same. 但是,如果它与您在第一个示例中运行的SQL完全相同,那么它有点奇怪,因为它们应该表现相同。 You should add the SQL script contents to the question to see what might be wrong. 您应该将SQL脚本内容添加到问题中以查看可能出错的内容。

The only thing I can think of that would change behaviour like that is if you had a login.sql that includes a set sqlterminator command, but you'd have to be picking up different login.sql files from the two calls... which is plausible if powershell has its own environment variables, perhaps. 我唯一能想到的就是改变这种行为,如果你有一个包含set sqlterminator命令的login.sql ,但是你必须从两个调用中获取不同的login.sql文件...如果powershell有自己的环境变量,那么这似乎是合理的。

It won't work this way. 它不会这样工作。 As Alex mentioned, sqlplus doesn't return anything as a function - it only writes whatever output you generate to standard output. 正如Alex所说,sqlplus不会将任何内容作为函数返回 - 它只会将您生成的任何输出写入标准输出。

You can have variables in sqlplus, but there is no easy way to pass them to host environment. 您可以在sqlplus中包含变量,但没有简单的方法将它们传递给主机环境。 The only way I can think of is to use spool command to generate another batch file, which will actually set your host variables, and then process them the way you want in your host script. 我能想到的唯一方法是使用spool命令生成另一个批处理文件,该文件实际上会设置您的主机变量,然后按照您希望的方式在主机脚本中处理它们。

Something like this: 像这样的东西:

16:30:20 SYSTEM@sandbox> get host.sql
  1  SET VERIFY OFF TRIMSPOOL ON TERMOUT OFF HEADING OFF LINESIZE 4000 PAGES 0 FEEDBACK OFF timing off
  2  spool s:\.tmp\host.ps1
  3  select '$env:MY_VAR="'||dummy||'"' from dual;
  4  spool off
  5* exit
16:30:25 SYSTEM@sandbox> @host.sql
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production
PS S:\.tmp> cat host.ps1
$env:MY_VAR="X"
PS S:\.tmp> .\host.ps1
PS S:\.tmp> $env:my_var
X
PS S:\.tmp>
sqlplus -s /nolog <<EOF > query_result.txt 
set heading off feedback off pagesize 0 linesize 30000 trimout on ;

select 5 from dual;
exit;
EOF

for i in `cat query_result.txt `
do
exit $i
done

try adding SET HEADING OFF in the beginning of your file. 尝试在文件的开头添加SET HEADING OFF Check this answer (check the second best voted answer) 检查这个答案(检查第二个最佳投票答案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM