#* FileName: Compare-QueryResults #*============================================================================= #* FUNCTION LISTINGS #*============================================================================= #* Function: Compare-QueryResults #* Created: [07/15/08] #* Author: Steven Murawski #*============================================================================= #* Purpose: #* #* #*============================================================================= param( $FirstQuery='path to first query file', $SecondQuery='path to second query file', $server = 'target database server', $database = 'target database name' ) #This function breaks each data row up into an object for each field in the row. #the name property is the column name and the value property is the field value #This allows compare-object to function DataRowToCompareableObject() { Process { $_.psobject.properties | select name, value } } $sql1 = get-content $FirstQuery | %{$txt=""}{$txt+="$_ `n"}{$txt} $sql2 = get-content $SecondQuery | %{$txt=""}{$txt+="$_ `n"}{$txt} #Get Values to compare $FieldValues1 = Run-SQLQuery -SqlServer $server -SqlCatalog $database -sqlquery $sql1 | DataRowToCompareableObject $FieldValues2 = Run-SQLQuery -SqlServer $server -SqlCatalog $database -sqlquery $sql2 | DataRowToCompareableObject #Get the value for the sync window (how far compare object will try to find a match) equal to half the larger count if ($FieldValues1.count -gt $FieldValues2.count) { $script:sync = $FieldValues1.count/2 } else { if ($FieldValues2.count -gt $FieldValues1.count) { $script:sync = $FieldValues2.count/2 } else { $script:sync = 10 } } #make the comparison Compare-Object $FieldValues1 $FieldValues2 -property name, value -syncWindow $sync