0

I have 2 while loops, one inside the other, that iterate over 2 different datasets and I want the outer while loop's first row data to loop through all the rows in the inner while loop looking for a match and if not, move onto the second row in the outer loop, again looping through all the records in the inner while loop. However, after the first outer loop's record has looped through all the records in the inner loop, the second row in the outer loop does not loop through the records in the inner loop; it's as if the first attempt at going through all the records in the inner loop did not reset itself back to the beginning! Here is my code - can someone tell me where I am going wrong?

$PgConnString = "select pgName from table1"
$MsConnString = "SELECT msName FROM table2";

$PgDbConn = CreateDbConnection "Postgres";
$PgDbCmd = CreateDbCommand $PgConnString $PgDbConn;

$PgReaderOutput = $PgDbCmd.ExecuteReader();

$MsDbConn = CreateDbConnection "MSSql";
$MsDbCmd = CreateDbCommand $MsConnString $MsDbConn;

$MsReaderOutput = $MsDbCmd.ExecuteReader();

while ($PgReaderOutput.Read())
{
    $ReporterName = $PgReaderOutput.GetValue(0);

    Write-Output "Current Reporter is: $ReporterName";

    while ($MsReaderOutput.Read())
    {
        $CurrMsRec = $MsReaderOutput.GetValue(0);
        Write-Output "Current SQL record is: $CurrMsRec";

        if($CurrMsRec -eq $ReporterName)
        {
            $Match = 1
            Write-Output "Matched"
        }
        else
        {
            Write-Output "No Matched"
            $Match = 0;
        }
    }

    If($Match -eq 1)
    {
        Write-Output "The reporter: $ReporterName has not been sent an email; send them one!";
    }
}
Jig Bhakta
  • 13
  • 5
  • Looks like it is not possible via a method that resets or does something similar per this: http://stackoverflow.com/questions/16361672/how-to-make-datareader-start-reading-again-from-the-beginning-at-a-condition. – Jig Bhakta Nov 10 '14 at 16:07

1 Answers1

0

In your inner loop you go through all elements returned by $MsReaderOutput.Read(). When this inner loop is done the first time, you don't change anything about $MsReaderOutput in your outer loop, so there can't possibly be any more elements the next time you process it.

I assume you want to reset your $MsReaderOutput object before each inner loop starts, so you can start iterating over it again.

  • I do want to reset it. How do I do that? – Jig Bhakta Nov 10 '14 at 02:34
  • You asked where you were going wrong and I told you. I don't know anything about the CreateDbConnection and CreateDbCommand functions you are using here or what methods the returned object has. Like so many people you seem to become completely passive as soon as someone is helping you. Go find out how you can reset that reader object - surely you must have some kind of documentation? –  Nov 10 '14 at 12:00
  • Thanks. I thought you may have known the answer on how to reset it. I knew that it wasn't being reset but it looks like the fault is mine in not being more concise in my final statement in terms of wanting to know how to reset it vs where I may be going wrong. – Jig Bhakta Nov 10 '14 at 15:50