Alex Yeung
Alex Yeung

Reputation: 2515

Execute SQL*Plus from PowerShell

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!

Upvotes: 1

Views: 14863

Answers (2)

m-smith
m-smith

Reputation: 3081

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

Upvotes: 1

Alex Yeung
Alex Yeung

Reputation: 2515

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.

Upvotes: 3

Related Questions