Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

with powershell i'm passing queries to sql in a loop.  how can i cumulate what i'm receiving from sql (datatables) ?

Posted on 2016-11-18
2
Medium Priority
?
62 Views
Last Modified: 2016-11-28
hello,

in a loop i'm passing queries to sql. ok, i'm receiving the responses.
now i'm trying to group in a datatable all the respoinses i'm receiving which are datatables.
i've created the same datatable in order to be able to add to it but this is not working.

i need help.

$allmyResulst =
    $allResults = New-Object System.Data.DataTable
    $allResults.Columns.Add("PersonID",[System.Type]::GetType("System.Int32"))
    $allResults.Columns.Add("firstname",[System.Type]::GetType("System.String"))
    $allResults.Columns.Add("lastname",[System.Type]::GetType("System.String"))

foreach($aline in $allFileLines)
{
     $query = "Select PersonID, firstname, lastname, email from table where firstname='lala'
     $results = pass-queryagainstDBserver $theConnection $query
     # ^ i receive my results ok
     $allresults.rows.add($results)    <- this is not adding my new lines:
}

thank you in advance
0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 2000 total points
ID: 41893208
Without testing, it looks like you're trying to add a DataTable to DataTable.Rows (and they're different object types).

What object type is returned by your custom function, DataTable?

Do your results have the same number of columns, because it looks like you're selecting 4 columns and adding to a table with 3.

Try this, to create a compatible row and fill it with values from a differing table, then add it back to the main table.
foreach($aline in $allFileLines)
{
     $query = "Select PersonID, firstname, lastname, email from table where firstname='lala'"
     $results = pass-queryagainstDBserver $theConnection $query 
     # ^ i receive my results ok 
     foreach ($thisRow in $results.Rows)
     {
        $addRow = $allResults.NewRow()
        $addRow["PersonID"] = $thisRow.PersonID
        $addRow["firstname"] = $thisRow.firstname
        $addRow["lastname"] = $thisRow.lastname
        $allResults.Rows.Add($addRow)
     }
}

Open in new window

0
 

Author Comment

by:toshi_
ID: 41904057
Hello Dustin,

thank you for your reply,

indeed i had forgotten to  add a column.
ok, indeed i could copy one Datarow from one Datatable to the other. Creating a new datarow works.

thank you.

Toshi
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

660 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