Execute SQL*Plus from PowerShell

让人想犯罪 __ 提交于 2019-12-08 07:27:18

问题


I got two files in C:\temp\SQL\alex.sql and in C:\temp\alex.ps1.

In C:\temp\SQL\alex.sql, that is simply

select count(*) from user_tables;
quit;

In C:\temp\alex.ps1, that is

$cmd = "sqlplus";
$args = "user/password@server/sid @C:\temp\SQL\alex.sql";
&$cmd $args;

I tried the command in Command Prompt

sqlplus user/password@server/sid @C:\temp\SQL\alex.sql which executed perfectly!

The SQL file is not executed at all, but SQL*Plus help is shown.

What did I do wrong? Thanks!


回答1:


I found the solution myself

I use cmd.exe /c and alex.ps1 is now

$cmd = "cmd.exe";
$args = "/c sqlplus user/password@server/sid @C:\temp\SQL\alex.sql";
&$cmd $args;

Hope this help.




回答2:


To offer an alternative solution to @Alex Yeung, you can simply use the PowerShell & call command to run the statement outright, without the need to use cmd.exe:

&sqlplus user/password@server @C:\path\script.sql

Consider adding the following to the bottom of your script file to ensure SQLPlus is closed when it has finished running:

DISCO -- Disconnect
EXIT -- Exit SQLPlus


来源:https://stackoverflow.com/questions/9627225/execute-sqlplus-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!