Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Invoke-Sqlcmd Count

$
0
0
Hello,I searched for this on here and google and didn't see an exact answer to my question, so sorry if it's been asked and answered before. If I run a query using Invoke-Sqlcmd and put it in a variable: $results = Invoke-Sqlcmd -Query $query -ServerInstance $QueryServer -Database $QueryDB;and I get multiple rows back, I see that $results has a type of System.Array. I can get a count of the number of returned results by using $results.Length.However if I get only 1 row back, the Type is Syste.Data.DataRow and $results.Length is NULLI've gotten around this by creating a new variable called ResultsLen and doing the following:$resultsLen = $results.<field in query>.Length$resultsLen += $results.Lengthbut this doens't really work, as the $results.<field in query>.Length is a count of the number of characters in the returned field. this works for what I need...which is just to run a IF statement to only run some code if there are 1+ records returned in the query...but I'm wondering if there is a better way of doing it?Thanks.

Viewing all articles
Browse latest Browse all 467

Latest Images

Trending Articles



Latest Images