简体   繁体   English

从PowerShell执行SQL * Plus

[英]Execute SQL*Plus from PowerShell

I got two files in C:\\temp\\SQL\\alex.sql and in C:\\temp\\alex.ps1 . 我在C:\\temp\\SQL\\alex.sqlC:\\temp\\alex.ps1有两个文件。

In C:\\temp\\SQL\\alex.sql , that is simply C:\\temp\\SQL\\alex.sql ,这很简单

select count(*) from user_tables;
quit;

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

$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! sqlplus user/password@server/sid @C:\\temp\\SQL\\alex.sql完美执行!

The SQL file is not executed at all, but SQL*Plus help is shown. 完全不执行SQL文件,但显示SQL * Plus帮助

What did I do wrong? 我做错了什么? Thanks! 谢谢!

I found the solution myself 我自己找到了解决方案

I use cmd.exe /c and alex.ps1 is now 我使用cmd.exe /calex.ps1现在

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

Hope this help. 希望能有所帮助。

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 : 要为@Alex Yeung提供替代解决方案,只需使用PowerShell & call命令直接运行该语句,而无需使用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: 请考虑将以下内容添加到脚本文件的底部,以确保SQLPlus在完成运行后关闭:

DISCO -- Disconnect
EXIT -- Exit SQLPlus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM