SQL Data Compare - Some tables missing

无人久伴 提交于 2019-12-05 01:01:47

Data can be compared only if you know what records from tables must be compared. Comparer uses PK to know what records to compare. If your table doesn't have a PK (or at least a unique index) it ill be missing from the tables list.

You can solve it by creating a PK yourself (just for comparison)

EDIT

  1. A coworker got a hard time recently due to someone explicit excluding some tables from the comparison project and committing it to git. So check it if it's not a new project.

  2. I recently was tasked to compare tables without PK and found HASHBYTES to be a new friend, also there are not unique rows in the tables and to solve it I used ROW_NUMBER with PARTITION, see below snipet.

    SELECT Row_number() 
             OVER ( 
               partition BY [hashid] 
               ORDER BY [hashid]) AS RowNumber, 
           * 
    INTO   [dbo].[mytable_temp] 
    FROM   (SELECT Hashbytes('SHA2_512', (SELECT x.* 
                                          FOR xml raw)) AS [HASHID], 
                   * 
            FROM   [dbo].[mytable] AS x) AS y 

    go 

    ALTER TABLE [dbo].[mytable_temp] 
      ALTER COLUMN [hashid] VARBINARY(900) NOT NULL 

    ALTER TABLE [dbo].[mytable_temp] 
      ALTER COLUMN [rownumber] BIGINT NOT NULL 

    go 

    ALTER TABLE [dbo].[mytable_temp] 
      ADD CONSTRAINT pk_id PRIMARY KEY ([hashid], [rownumber]) 

    go 

That way I can create PK based on a hash calculated with the entire row content.

Obs:. Note I'm comparing MyTable_TEMPand not MyTable. that way I can leave it untounched.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!