Compare two rows

  • Looking for easy ways to compare two rows?

  • You need to get familiar with the system tables within SQL server. then you can easily create utility procs to do things like this for you:

     

    DECLARE @collist VARCHAR(3000)

    select @collist = ISNULL(  @collist + CHAR(13) + CHAR(10) + ',' , '' ) + 'CASE WHEN a.' + c.name + ' <> b.' + c.name + ' THEN ''Y'' ELSE '' '' END AS ' + c.name

    from syscolumns c

         JOIN sysobjects o on o.id = c.id

    where o.name = 'sysobjects'

    ORDER BY colid

    SELECT @collist = 'SELECT ' + @collist + ' FROM sysobjects a JOIN sysobjects b ON a.id = 1 and b.id = 2'

    --SELECT  @collist

    PRINT 'COLUMNS THAT ARE DIFFERENT Between sysobjects id 1 and sysobjects id 2: '

    EXEC ( @collist  )

  • Data Compare from Red Gate. Cheap, easy and works well.

    http://www.red-gate.com/SQL_Data_Compare.htm

  • REDGate SQL Data Compare is great, but if you are budget constrained and can't procure that use the built-in BINARY_CHECKSUM function:

    DECLARE @BC1 INT, @BC2 INT

    SELECT @BC1=BINARY_CHECKSUM(*) FROM <table> WHERE <clause to get 1st row>

    SELECT @BC2=BINARY_CHECKSUM(*) FROM <table> WHERE <clause to get 2nd row>

    IF @BC1 <> @BC2 PRINT '2 rows are different'

    Jeff

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply