I'm trying to implement a methodology to check the integrity of a restored database. It goes as following:
- Create a snapshot of the database.
- Gather various metadata:
- On disk used space.
- Name of tables.
- Number of rows in each tables.
- ...
- Generate a cryptographic hash using the data in each table.
My issue goes with the 3rd step to generate the cryptographic hash. I plan to read each row one by one and update my hash with the data they contain. But for my check to match I have to make sure that the row order stays the same.
How can I make sure to always get the same row order, assuming that I don't know upfront the table structure and on what column I can order on.
CHECKSUM_AGG
hardly qualifies as cryptographic strength though, performing a simple CRC routine and returning a 4-byte result. See stackoverflow.com/questions/26221210 amongst other discussions. Though IIRC that is the only hash available as a convenient aggregate function in TSQL (without using CLR code to implement your own).