• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6362
  • Last Modified:

Retrieving data using select SQL statement in Powershell

This URL stackoverflow.com/questions/1758779/retrieving-data-using-select-sql-statement-in-powershell helped in retrieving data from SQL query into a variable.
I would like to enhance this to retrieve multiple values.

$SqlCmd.CommandText = "select column1,column2 from table1"
$column1 = $SqlCmd.ExecuteScalar()

How can I assign column2 value into a variable.

Thanks
Sharath
0
Sharath
Asked:
Sharath
  • 2
1 Solution
 
Raheman M. AbdulCommented:
Try the following:
Executescalar returns only one value
Executereader returns multiple values
------------------------------------------------------------------------
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = "select column1,column2 from table1"

$Reader = $Command.ExecuteReader()
$column1 = $Reader.GetValue(0)
$column2 = $Reader.GetValue(1)
0
 
SharathData EngineerAuthor Commented:
will get back on this
0
 
SharathData EngineerAuthor Commented:
Thanks for pointing to use Executereader. Somehow, I am getting issues with this. I used a different approach to get the work done. Thanks.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now