Posts

Showing posts with the label compare

Compare data in all user tables between two databases

/* This script-generating script will output t-sql script for comparing data in tables for two databases. Some columns with special data types are excluded. Maybe they can be casted to varchar, but its not done here. Tip: Execute this script in the database with least tables. Remember to run output script in both directions since it uses "except" (switch db1 db2). Will report diff row count and rows themselves. Can be heavy if big db.   Use "Copy with headers" in result below for correct script Written on MsSql 2008r2 */ declare  @db1 varchar(100) = 'Db1', @db2 varchar(100) = 'Db2' declare @excludedTypes as table (TypeName varchar(256))declare @excludedColumns as table (ColumnName varchar(256)) insert @excludedTypes values ('image'),('text'),('ntext'),('xml'),('timestamp') insert @excludedColumns values ('ColumnToIgnore1'),('ColumnToIgnore2') select distinct   'select '+ ...

hashbytes

/* Checksum() isnt very good for detecting data changes. Use hashbytes() instead Example of output from script below: -361975519341300.801789 */ select avg(cast(cast(hsh as bigint) as decimal(25,0))) as truncatedHash from ( select cast(HASHBYTES('SHA1', [StringField1]+ [StringField2]+ [StringField3]) as binary(20)) as hsh FROM dbo.TableToBeInvestigated ) as hashedData