Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaneis/3e4d16703c4a4bf122ab146e497a647b to your computer and use it in GitHub Desktop.
Save shaneis/3e4d16703c4a4bf122ab146e497a647b to your computer and use it in GitHub Desktop.
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