Oracle 如何在powershell变量中捕获PL SQL函数的输出?

Oracle 如何在powershell变量中捕获PL SQL函数的输出?,oracle,powershell,plsql,sqlplus,Oracle,Powershell,Plsql,Sqlplus,我能够使用powershell和sqlplus连接到远程oracle数据库。此外,我还能够将数据库中执行的命令/查询的输出捕获到powershell变量中。 PFB示例 $username ="hr" $password ="hr" $tnsalias ="orcl" $outputfilepath="C:\Users\Desktop" $sqlquery=@" set serveroutput off set feedback off set heading off set ech

我能够使用powershell和sqlplus连接到远程oracle数据库。此外,我还能够将数据库中执行的命令/查询的输出捕获到powershell变量中。 PFB示例

$username ="hr"
$password ="hr"
$tnsalias ="orcl"
$outputfilepath="C:\Users\Desktop"

 $sqlquery=@"
 set serveroutput off
 set feedback off
 set heading off
 set echo off
 select sysdate from dual;
"@
在oracle数据库中运行上述查询,如下所示

 $xval=$sqlquery | sqlplus -silent $username/$password@$tnsalias
 echo $xval
输出将是

23-APR-18
问题是当我运行一个PL SQL函数时,如下所示

    $sqlquery=@"
     set serveroutput off
     set feedback off
     set heading off
     set echo off
     declare
     x varchar2:=null;     
     exec x:=MYTEST_FUN('a','b');
    "@
$xval
变量中未捕获任何值,而且函数未运行


如何执行函数,以使函数的返回值被捕获到powershell变量中。

一种方法是使其
将serveroutput设置为on
并使用
DBMS\u OUTPUT.PUT\u LINE()
开始..结束

另一个选项是使用sqlplus'
PRINT
命令

set serveroutput off
set feedback off
set heading off
set echo off
variable x VARCHAR2  --declare bind variable x
exec  :x := MYTEST_FUN('a','b'); --a colon before x needed.
PRINT x;
为我工作:

$sqlQuery = @"
    set serveroutput off
    set feedback off
    set heading off
    set echo off
    variable x NUMBER;
        select count(*) into :x from TABLE;
    PRINT x;
    exit
"@
($sqlQuery | sqlplus -S user/pass | Write-Output | Out-String).Trim()

最简洁地说,延迟输入:

$dateVar = ("set heading off
            select sysdate from dual;" | sqlplus -s $yourConnectionString | Out-String).Trim()

通过打印绑定变量值,我可以在powershell变量中捕获它。@rainu:我忘了提一件事。您还应该更喜欢使用合适的大小声明
VARCHAR2
,以适合您的输出,比如
VARCHAR2(10)
是的,我必须这样做。