Solved

Retrieving data using select SQL statement in Powershell

Posted on 2013-11-25
3
4,903 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now