Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell Method Table Column Differences
$Table01Param = @{
ServerInstance = 'localhost\SQLServer2K16'
Database = 'Pantheon'
Query = "SELECT OBJECT_NAME(object_id) AS TableName, name AS ColumnName FROM sys.columns WHERE object_id = OBJECT_ID(N'dbo.TableColumnDifference01', N'U')"
}
$Table02Param = @{
ServerInstance = 'localhost\SQLServer2K16'
Database = 'Pantheon'
Query = "SELECT OBJECT_NAME(object_id) AS TableName, name AS ColumnName FROM sys.columns WHERE object_id = OBJECT_ID(N'dbo.TableColumnDifference02', N'U')"
}
$Table1 = Invoke-Sqlcmd @Table01Param
$Table2 = Invoke-Sqlcmd @Table02Param
Compare-Object -ReferenceObject $Table1 -DifferenceObject $Table2 -Property ColumnName -IncludeEqual |
Select-Object -Property *,
@{label = 'HRF'; expression = {switch ($_.SideIndicator) {
'==' { 'Both' }
'=>' { 'Table 2 Only' }
'<=' { 'Table 1 Only'}
}}
} |
Sort-Object -Property ColumnName;
# Stats please -- basically how long it takes
Measure-Command {
Compare-Object -ReferenceObject $Table1 -DifferenceObject $Table2 -Property ColumnName -IncludeEqual |
Select-Object -Property *,
@{label = 'HRF'; expression = {switch ($_.SideIndicator) {
'==' { 'Both' }
'=>' { 'Table 2 Only' }
'<=' { 'Table 1 Only'}
}}
} |
Sort-Object -Property ColumnName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.