如何使用 Windows PowerShell 在不退出的情况下将输入重定向到 SQL*Plus
How to Redirect Input to SQL*Plus without Exiting using Windows PowerShell
使用 Windows PowerShell,如何将输入重定向到 sqlplus
(非交互式标准输入模式),以便在重定向输入完成后 sqlplus
在交互式标准输入中保持打开状态没有 SQL*Plus 退出的模式?
似乎重定向输入正在发出隐式 exit
,SQL*Plus 正在处理。
使用 Oracle 19c 企业版、Oracle InstantClient 19c 64 位、Windows PowerShell Desktop 5.1.19041.1320、Microsoft Windows 10.0 build 19042。
提前谢谢你。
dev.sql
set echo on
set serveroutput on
exec dbms_output.put_line('hello world');
无重定向执行启动脚本 dev.sql
并 not 退出(注意 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>
重定向 here-string.
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
,我完成了”。也许有一种方法可以在 pipped 输入到达文件末尾时将 stdin
引导回他们的键盘,所以 sqlplus
会等待下一个键盘输入?
注意:奇怪的是我们没有看到 set serveroutput on
得到回应。
谢谢。
Sqlplus 在其输入管道 (stdin) 关闭时自动退出。因此,唯一的选择是不关闭管道:您可以编写一个程序来启动 sqlplus 并将其提供给标准输入,并仅在需要时关闭它。
我不知道你为什么要让它在 non-interactive 模式下打开。
对我来说,通过管道传输到文件然后执行它要好得多。
不知道它是否对您有帮助,但我有几个解决方法可以让它保持打开状态一段时间:使用 host
命令。例如,以下命令将使 sqlplus 打开 15 秒:
"host powershell Start-Sleep -s 15" | sqlplus user/pass@//host:port/service
请注意,您无法使用其中的交互式标准输入执行任何操作,例如
"host timeout /t 10" | sqlplus user/pass@//host:port/service
因为
SQL> ERROR: Input redirection is not supported, exiting the process immediately.
使用 Windows PowerShell,如何将输入重定向到 sqlplus
(非交互式标准输入模式),以便在重定向输入完成后 sqlplus
在交互式标准输入中保持打开状态没有 SQL*Plus 退出的模式?
似乎重定向输入正在发出隐式 exit
,SQL*Plus 正在处理。
使用 Oracle 19c 企业版、Oracle InstantClient 19c 64 位、Windows PowerShell Desktop 5.1.19041.1320、Microsoft Windows 10.0 build 19042。
提前谢谢你。
dev.sql
set echo on
set serveroutput on
exec dbms_output.put_line('hello world');
无重定向执行启动脚本 dev.sql
并 not 退出(注意 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>
重定向 here-string.
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
,我完成了”。也许有一种方法可以在 pipped 输入到达文件末尾时将 stdin
引导回他们的键盘,所以 sqlplus
会等待下一个键盘输入?
注意:奇怪的是我们没有看到 set serveroutput on
得到回应。
谢谢。
Sqlplus 在其输入管道 (stdin) 关闭时自动退出。因此,唯一的选择是不关闭管道:您可以编写一个程序来启动 sqlplus 并将其提供给标准输入,并仅在需要时关闭它。
我不知道你为什么要让它在 non-interactive 模式下打开。 对我来说,通过管道传输到文件然后执行它要好得多。
不知道它是否对您有帮助,但我有几个解决方法可以让它保持打开状态一段时间:使用 host
命令。例如,以下命令将使 sqlplus 打开 15 秒:
"host powershell Start-Sleep -s 15" | sqlplus user/pass@//host:port/service
请注意,您无法使用其中的交互式标准输入执行任何操作,例如
"host timeout /t 10" | sqlplus user/pass@//host:port/service
因为
SQL> ERROR: Input redirection is not supported, exiting the process immediately.