Compare SQL Tables II by Bernd Kriszio 31 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2390"></script>download | new post
Another version to compare 2 SQL Tables. Uses code to exclude specified columns from SELECT *
- function Convert-TableToList
- {
- param(
- $t,
- $colid = 0
- )
- $t | % {$_.item($colid)}
- }
- function Compare-Tables
- {
- param(
- $name,
- $db1,
- $db2,
- $exclude = @()
- )
- # @bernd_k http://pauerschell.blogspot.com/
- # requires on sqlise http://sqlpsx.codeplex.com/
- $sql = "select name from sys.columns where object_id = object_id('$db1..$name') order by column_id"
- Invoke-ExecuteSql $sql 'variable' columns
- $columns = Convert-TableToList $columns | % { if ($exclude -notcontains $_) {$_} }
- $columnlist = $columns -join ', '
- $sql = @"
- Select 1 [table], $columnlist from $db1..$name
- except
- Select 1 [table], $columnlist from $db2..$name
- union
- Select 2 [table], $columnlist from $db2..$name
- except
- Select 2 [table], $columnlist from $db1..$name
- ORDER by 2
- "@
- $sql
- Invoke-ExecuteSql $sql 'grid'
- }
- # Compare-Table2 sometable db1 db2 -ex @('colx', 'coly')
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository