Solved

Retrieving data using select SQL statement in Powershell

Posted on 2013-11-25
3
5,175 Views
Last Modified: 2013-12-16
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
Comment
Question by:Sharath
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39676076
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
 
LVL 40

Author Comment

by:Sharath
ID: 39710292
will get back on this
0
 
LVL 40

Author Closing Comment

by:Sharath
ID: 39722886
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question