使用 Windows PowerShell,如何將輸入重定向到sqlplus(非互動式標準輸入模式),以便在重定向輸入完成后sqlplus在互動式標準輸入模式下保持打開狀態,而不退出 SQL*Plus?
看起來好像重定向的輸入正在發出exitSQL*Plus 正在處理的隱含資訊。
使用 Oracle 19c 企業版、Oracle InstantClient 19c 64 位、Windows PowerShell Desktop 5.1.19041.1320、Microsoft Windows 10.0 build 19042。
先感謝您。
開發者.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>
However, when redirecting the input, then SQL*Plus automatically exits (notice the PS C:\Users\my> prompt as the last line instead of SQL>). No matter how I redirect the input into sqlplus, SQL*Plus automatically exits.
Redirection with 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>
Redirection with 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>
Single line redirection.
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>
Redirect using 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>
Redirecting using batch, still causes SQL*Plus to exit.
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>
Note: I tried using -noexit but that is an argument to executing powershell itself not sqlplus.
It is like sqlplus says "okay, no more stdin, I'm done". Maybe there is a way to direct stdin back to they keyboard once pipped input reaches end-of-file, so sqlplus is left waiting for the next keyboard input?
注意:我們沒有看到set serveroutput on得到回應也很奇怪。
謝謝你。
uj5u.com熱心網友回復:
Sqlplus 在其輸入管道 (stdin) 關閉時自動退出。所以唯一的選擇是不要關閉管道:你可以撰寫一個程式來啟動 sqlplus 并提供它的標準輸入,并且只在你想要的時候關閉它。
我不知道你為什么要讓它在非互動模式下打開。對我來說,通過管道傳輸到檔案然后執行它要好得多。
不知道它會幫助你,但我有幾個解決方法如何讓它打開一段時間:使用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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/425299.html
標籤:视窗 甲骨文 管道 io重定向 powershell-5.1
