mhopkins321
mhopkins321

Reputation: 3073

Saving result of COUNT() sql query into variable in powershell

I am running an sql query that will save the result into a csv. The only thing in the csv is the return of a COUNT() function. Is there a way that I can save this number directly into a variable in powershell?

Upvotes: 2

Views: 2309

Answers (2)

Joey
Joey

Reputation: 354576

If you want to get rid of the CSV you could use the SqlServerCmdletSnapin:

Add-PSSnapIn SqlServerCmdletSnapin100
(Invoke-Sqlcmd -server .\sqlexpress -database foo -query 'SELECT COUNT(...) FROM ...')[0]

Upvotes: 1

Keith Hill
Keith Hill

Reputation: 201672

Should be simple. Let's assume the CSV field is called Result:

$result = (Import-Csv .\sqlout.csv).Result

Upvotes: 2

Related Questions