Forum Discussion

akau1902's avatar
akau1902
Copper Contributor
Dec 08, 2020

Powershell only executes first sql statement in the sql file

Hello Everyone,

 

I am trying execute two simple SELECT statements placed in a sql file and then trying to execute the sql file via powershell and dumping the results in a text file using the below code. However it only executes the first select statement.

 

$SQL = Get-Content -Path "C:\SQL Reports\Daily Checks.sql"

Invoke-Sqlcmd -Username "*****" -Password "*****" -ServerInstance "*****" -Database "*****" -Query $SQL | Out-File -FilePath "C:\SQL Reports\powershelloutput.txt"

 

It is just printing the results of the first sql in the output file and not the second one.

Can someone please guide what may be wrong or if the above method is not supposed to execute multiple sqls in a sql file.

 

The simple sql statements are like below

 

SELECT * FROM TABLE1

SELECT * FROM TABLE2

 

Thanks,

AK

 

Resources