0

So i have the following loop:

foreach ( $item in $mergeJobs.JobID ) {
    $mainExists = Invoke-SQLCmd -Query "SELECT COUNT(*) FROM JobCandidate WHERE JobID = $item AND CandidateID = $($mainCandidate.itemArray[0])" -ServerInstance $ServerAddress -database $DatabaseName
    Write-Host "SELECT COUNT(*) FROM JobCandidate WHERE JobID = $item AND CandidateID = $($mainCandidate.itemArray[0])"
    Write-Host "MainExists = $($mainExists.itemArray[0])"
    pause
    if ( $mainExists.itemArray[0] = 1 ) {
        Write-Host "somehow here"
        $isExists = 1
        interviewMerge $mainCandidate.itemArray[0] $toMerge[$i][0] $databaseName $ServerAddress $isExists $item

        Invoke-SQLCmd -Query "DELETE FROM interview WHERE JobCandidateID IN (SELECT JobCandidateID FROM JobCandidate WHERE jobID = $item AND CandidateID = $toMergeID)" -ServerInstance $ServerAddress -database $DatabaseName
        Invoke-SQLCmd -Query "DELETE FROM JobCandidate WHERE JobID = $item AND CandidateID = $toMergeID" -ServerInstance $ServerAddress -database $DatabaseName    
    }
    if ( $mainExists.itemArray[0] = 0 ) {
        Write-Host " here"

        $isExists = 0

        Invoke-SQLCmd -Query "DELETE FROM interview WHERE JobCandidateID IN (SELECT JobCandidateID FROM JobCandidate WHERE jobID = $item AND CandidateID = $toMergeID)" -ServerInstance $ServerAddress -database $DatabaseName
        Invoke-SQLCmd -Query "UPDATE JobCandidate SET candidateID = $($mainCandidate.itemArray[0]) WHERE JobID = $item AND CandidateID = $toMergeID" -ServerInstance $ServerAddress -database $DatabaseName

        interviewMerge $mainCandidate.itemArray[0] $toMerge[$i][0] $databaseName $ServerAddress $isExists $item
    }
}

This isn't evaluating properly it always seems to think $mainExists.itemArray[0] is equal to 1, in the if statement anyway the Write-Host call beforehand shows it as 0. here is some sample output:

SELECT COUNT(*) FROM JobCandidate WHERE JobID = 1224 AND CandidateID = 6239
MainExists = 0 ## first call, shows that the query above evaluates to 0
Press Enter to continue...:
somehow here
1   ## prints isExists in the function call
1224

This, I'm sure will be an easy fix but I can't seem to figure it out ( i have tried with else and elseif too, same result )

Owain Esau
  • 1,876
  • 2
  • 21
  • 34
  • 5
    Change `if ( $mainExists.itemArray[0] = 1 )` to `if ( $mainExists.itemArray[0] -eq 1 )` and the other IF. For most other languages, that `=` would be `==`. easy typo to make in PowerShell. – Kory Gill Feb 07 '18 at 01:12
  • I realized after posting that it needed to be > not == since its a count and not always going to be 1. realized what I was doing wrong the second I typed that > . thanks for the help. – Owain Esau Feb 07 '18 at 01:18
  • 2
    Remember in PowerShell that is -eq -ne -ge -gt -lt -le. – Kory Gill Feb 07 '18 at 01:21

0 Answers0