hash a SQL row?

后端 未结 4 577
情歌与酒
情歌与酒 2020-12-29 23:32

Is there a way to md5sum a row in a SQL table to check whether any column has been modified?

I would like to check whether any particular column has been changed ver

4条回答
  •  温柔的废话
    2020-12-30 00:02

    I had to develop a solution to compare table structures and run a procedure to import the difference between the tables.

    I used below code to select the data

    --> table structures create table #table1 ( campo varchar(10) ,campo1 varchar(10) )

    create table #table2 (
    campo varchar(10)
    ,campo1 varchar(10)
    )
    

    --> Insert values

    insert into #table1 values ('bruno',1)
    insert into #table1 values ('bruno',2)
    insert into #table2 values ('bruno',1)
    insert into #table2 values ('bruna',2)
    

    --> Create a hash column to compare

    select *,HASHBYTES('SHA1', (select z.* FOR XML RAW)) as hash
        into #compare1
     from #table1 z 
    
    select *,HASHBYTES('SHA1', (select k.* FOR XML RAW)) as hash
        into #compare2
     from #table2 k 
    

    --> check the lines that has any difference

    select * from  #compare1 a
    full outer join #compare2 b on a.hash = b.hash
    where ( a.hash is null or b.hash is null )
    

    Maybe this is useful for someone needing the same thing

提交回复
热议问题