Have you ever been blind sided by a database schema change in your production enviorment? It is no fun!
You may work in a enviorment when changes are few and far between or hundreds per day, either way it would be great to automatically detect changes.
Today I am going to explain how to do just that! Lets get started, here is the Full Rexy.
Lets start with this basic query to the sys objects and sys columns DMVs. This query will return the tablename, name of column, column id ,column type and the length.
This will be the foundation of our query to detect changes, we still need to do some work to get the exact result we want.
Next we need to add a subquery to this base query to blend all of those rows into a single row.
To accomplish this we will utalize the STUFF and For XML path trick seperating each with a comma.
Now we have a single row comma seperated of every column for each table in the database we are connected to.
We need to parameterize and get rid of the leading comma, this is where stuff comes in. It works similar to substring,
and concatenation in a single function. Lets also go ahead and roll the entire function into a scalar function. The final resulting function looks as follows:
Lastly its time to test the function, lets create a table, plug in the table name, modify the table and compare checksum values.
As you can see for the first run we get a check sum for the first run is 55FD251F00F8E1EB7A2B7DCBF75A3D30
The second run is 4E1BA8DA9302CE7DD21949454D39F22A.
This technique will work for any column changes, if the order, datatype or length changes the checksum will be different!
You could expand this function to alert via teams or an email notification to keep you in the know when these changes occur.