如果语句未正确评估 - invoke-sqlcmd

时间:2018-02-07 01:08:45

标签: powershell invoke-sqlcmd

所以我有以下循环:

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
    }
}

这还没有正确评估似乎总是认为$mainExists.itemArray[0]等于1,在if语句中,无论如何Write-Host调用事先将其显示为0.这里是一些示例输出:

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

这一点,我确定会很容易解决,但我似乎无法解决这个问题(我已经尝试过使用else,而且也尝试过,同样的结果)

0 个答案:

没有答案