Quantcast
Channel: Active questions tagged powershell-3.0 - Stack Overflow
Viewing all articles
Browse latest Browse all 122

How to catch sql query error in powershell

$
0
0

I have a query like this in my PowerShell Script

try{    $Query = "SELECT ID / 0 FROM Tables1"    Invoke-Sqlcmd -Query $Query -ServerInstance $Global:Server -Database $Global:Database}catch {"error when running sql $Query "    $error=  $_}

The problem I'm having is when I run this query in Azure Data Studio then I get an error and that look fine but

SELECT ID / 0 FROM Table1Msg 8134, Level 16, State 1, Line 1Divide by zero error encountered.

when I run the query inside my trycatch block ins my PowerShell script then some reason it's not outputting the error even though I wrap it inside trycatch block.

So I'm just wondering how could I catch the sql statement errors in my PowerShell Script?


Viewing all articles
Browse latest Browse all 122

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>