使用PowerShell,如何将输入重定向到sqlplus (非交互式stdin模式),这样一旦重定向输入完成,sqlplus就在交互式stdin模式下打开,而不退出SQL*Plus?
似乎重定向的输入正在发出一个隐式exit,而SQL*Plus正在处理该exit。
使用Oracle19c企业版,Oracle InstantClient 19c 64位,PowerShell桌面5.1.19041.1320,MicrosoftWindows10.0构建19042。
提前谢谢你。
dev.sql
set echo on
set serveroutput on
exec dbms_output.put_line('hello world');没有重定向执行启动脚本dev.sql并执行而不是退出(注意SQL>提示符是最后一行而不是PS C:\Users\my>)。
PS C:\Users\my> sqlplus my_username/my_password@"my_host:my_port/my_service" "@dev.sql"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 14:44:48 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 14:40:03 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> set serveroutput on
SQL> exec dbms_output.put_line('hello world');
hello world
PL/SQL procedure successfully completed.
SQL>但是,当重定向输入时,SQL*Plus会自动退出(注意PS C:\Users\my>提示符是最后一行,而不是SQL>)。无论我如何将输入重定向到sqlplus,SQL*Plus都会自动退出。
用Get-Content重定向。
PS C:\Users\my> Get-Content dev14.sql | sqlplus my_username/my_password@"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 14:48:42 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 14:44:49 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>用这里-字符串重定向。
PS C:\Users\my> @"
>> set echo on
>> set serveroutput on
>> exec dbms_output.put_line('hello world');
>> "@ | sqlplus my_username/my_password@"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 14:50:14 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 14:48:44 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>单线重定向。
PS C:\Users\my> "select * from dual;" | sqlplus my_username/my_password@"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 15:03:10 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 15:00:34 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL>
D
-
X
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>使用Start-Process重定向。
PS C:\Users\my> Start-Process sqlplus my_username/my_password@"my_host:my_port/my_service" -RedirectStandardInput dev.sql -NoNewWindow -Wait
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 15:06:20 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 15:06:07 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>使用批处理重定向,仍然会导致SQL*Plus退出。
dev.bat
(
echo set echo on
echo set serveroutput on
echo exec dbms_output.put_line('hello world'^^^);
) | sqlplus my_username/my_password@"my_host:my_port/my_service"PS C:\Users\my> c:dev.bat
C:\Users\my>(
echo set echo on
echo set serveroutput on
echo exec dbms_output.put_line('hello world'^);
) | sqlplus my_username/my_password@"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 15:57:51 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 15:57:38 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>注意:我尝试使用-noexit,但这是执行powershell本身的一个参数,而不是sqlplus。
就像sqlplus说的“好的,不再是stdin,我受够了”。也许有一种方法可以让stdin回到他们的键盘上,一旦输入到文件末尾,那么sqlplus就会等待下一个键盘输入吗?
注意:我们怎么没有看到set serveroutput on得到回响,这也是很奇怪的。
谢谢。
发布于 2022-02-15 22:53:28
当它的输入管道(stdin)关闭时,Sqlplus会自动退出。因此,唯一的选择是不要关闭管道:您可以编写一个程序,启动sqlplus并提供它的stdin,并且只在您需要的时候关闭它。
我不知道你为什么要让它在非交互模式下打开。对我来说,用管道传送文件,然后执行它要好得多。
不知道它对您有帮助吗,但是我有几个解决方法--如何将它打开一段时间:使用host命令。例如,以下命令将使sqlplus打开15秒:
"host powershell Start-Sleep -s 15" | sqlplus user/pass@//host:port/service请注意,您不能在其中使用交互式stdin做任何事情,例如
"host timeout /t 10" | sqlplus user/pass@//host:port/service因为
SQL> ERROR: Input redirection is not supported, exiting the process immediately.https://stackoverflow.com/questions/71133682
复制相似问题