Powershell only executing first SQL in the SQL file

%3CLINGO-SUB%20id%3D%22lingo-sub-1965041%22%20slang%3D%22en-US%22%3EPowershell%20only%20executing%20first%20SQL%20in%20the%20SQL%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1965041%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22lia-quilt-row%20lia-quilt-row-header%22%3E%3CDIV%20class%3D%22lia-quilt-column%20lia-quilt-column-12%20lia-quilt-column-left%20lia-quilt-column-header-left%22%3E%3CDIV%20class%3D%22lia-quilt-column-alley%20lia-quilt-column-alley-left%22%3E%3CDIV%20class%3D%22lia-message-author-rank%20lia-component-author-rank%20lia-component-message-view-widget-author-rank%22%3E%3CSPAN%3EHello%20Everyone%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-quilt-row%20lia-quilt-row-main%22%3E%3CDIV%20class%3D%22lia-quilt-column%20lia-quilt-column-18%20lia-quilt-column-left%20lia-quilt-column-main-left%22%3E%3CDIV%20class%3D%22lia-quilt-column-alley%20lia-quilt-column-alley-left%22%3E%3CDIV%20class%3D%22lia-message-body%20lia-component-message-view-widget-body%20lia-component-body-signature-highlight-escalation%20lia-component-message-view-widget-body-signature-highlight-escalation%22%3E%3CDIV%20class%3D%22lia-message-body-content%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20execute%20two%20simple%20SELECT%20statements%20placed%20in%20a%20sql%20file%20and%20then%20trying%20to%20execute%20the%20SQL%20file%20via%20PowerShell%20and%20dumping%20the%20results%20in%20a%20text%20file%20using%20the%20below%20code.%20However%20it%20only%20executes%20the%20first%20select%20statement.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24SQL%20%3D%20Get-Content%20-Path%20%22C%3A%5CSQL%20Reports%5CDaily%20Checks.sql%22%3C%2FP%3E%3CP%3EInvoke-Sqlcmd%20-Username%20%22*****%22%20-Password%20%22*****%22%20-ServerInstance%20%22*****%22%20-Database%20%22*****%22%20-Query%20%24SQL%20%7C%20Out-File%20-FilePath%20%22C%3A%5CSQL%20Reports%5Cpowershelloutput.txt%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20just%20printing%20the%20results%20of%20the%20first%20sql%20in%20the%20output%20file%20and%20not%20the%20second%20one.%3C%2FP%3E%3CP%3ECan%20someone%20please%20guide%20what%20may%20be%20wrong%20or%20if%20the%20above%20method%20is%20not%20supposed%20to%20execute%20multiple%20sqls%20in%20a%20sql%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20simple%20SQL%20statements%20are%20like%20below%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20*%20FROM%20TABLE1%3C%2FP%3E%3CP%3ESELECT%20*%20FROM%20TABLE2%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EAK%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1965041%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eonly%20executing%20first%20SQL%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1969141%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20only%20executing%20first%20SQL%20in%20the%20SQL%20file%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1969141%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F894112%22%20target%3D%22_blank%22%3E%40akau1902%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EAs%20specified%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsqlserver%2Finvoke-sqlcmd%3Fview%3Dsqlserver-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Edocumentation%3C%2FA%3E%3A%3C%2FP%3E%3CP%3EWhen%20this%20cmdlet%20is%20run%2C%20the%20%3CSTRONG%3Efirst%20result%20set%20that%20the%20script%20returns%20is%20displayed%20as%20a%20formatted%20table%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%3CSTRONG%3EIf%20subsequent%20result%20sets%20contain%20different%20column%20lists%20than%20the%20first%2C%20those%20result%20sets%20are%20not%20displayed.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EIf%20subsequent%20result%20sets%20after%20the%20first%20set%20have%20%3CSTRONG%3Ethe%20same%20column%20list%2C%20their%20rows%20are%20appended%20to%20the%20formatted%20table%3C%2FSTRONG%3E%20that%20contains%20the%20rows%20that%20were%20returned%20by%20the%20first%20result%20set.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor
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

2 Replies

Hello @akau1902,

As specified in documentation:

When this cmdlet is run, the first result set that the script returns is displayed as a formatted table.

If subsequent result sets contain different column lists than the first, those result sets are not displayed.

If subsequent result sets after the first set have the same column list, their rows are appended to the formatted table that contains the rows that were returned by the first result set.

 

Hope that helps.

Hi Andy,

 

Thanks for reply and pointing to the documentation.

 

I can now see why it is behaving like this. I was however wondering why the same "Sqlcmd" command works the expected way when invoked via command prompt.

 

As a work around to above I now made a bat file and I am invoking the "sqlcmd" command within the bat file and it is giving me results for multiple SQL statements with different columns all appended in the one same text file, which is what I was looking for.

 

I just don't understand why it is that when same method is invoked via powershell, it is not able to execute in the same way..

 

Thanks,

Aiesh