I set up a SQL connection inside a Function. When I run the Function inside a variable, I get the correct query results, plus an additional result of ‘0’. $variable.count also gives me an additional value on top of the query results.
The function is not seen as an object in the variable. I’m not sure what that means, actually.
Your function is outputting $DataSet.Tables[0]. What happens if you just run your function, without assigning it to a variable? If you get nothing, then either:
Your query is wrong and it isn’t producing any results
Or
The data table couldn’t be filled before you closed the connection
If I run the Function by itself, the first thing it returns is the count of the objects returned.
The first ‘object’ in the results is the value ‘23’… followed by 23 actual results from my query. It is the correct number of results from just running the query in SQL.
When I put it in a variable and do $variable.count…it returns ‘24’.
So you’re probably just going to have to discard the first object, if that’s what’s being output. You’re always getting back one objects with the result count. You could do this in your function by piping your data table to " | Select-Object -skip 1" if you wanted.
I guess it makes sense to me. I will see about skipping the first result. The only bothersome thing about it is that extra result always fails down the pipeline. I guess my question was, ‘What is that extra object?’.
Hey @creed-cordonier,
That “extra object” you are seeing is actually the returned value from your $SqlAdapter.Fill($DataSet)
The fill method for the System.Data.SqlClient.SqlDataAdapter has a return value as follows
Return Value
Type: System.Int32
The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows.
Since you are not capturing that return value in a variable, or outputting it to Null like @its4kiran is in his example, the return value is sent to the default output.