It is a common and business requirement to backup the data. The archiving is part of back ups and most of the applications and system will be following some procedure to archive the data.
Since most of the application is having some kind of mechanism to archive data like SQL stored procedures, statements, Database bound applications to do the archiving.
I have seen having bigger and complex statements in SQL stored procedures to copy the data into archive tables based on some conditions, say created date etc.
But, the problem starts when we add tables or change structure of some tables. We often forget to update or create archiving tables / table structures, which intern will fail or miss out the newly created table or field data.
I was thinking to automate this process, so that, if any of the above said problems rises, the archiving process will continue.
I have created a stored procedure to do the same; this does the following things as validation and execution procedure
1. Check whether the archiving table exists for the original table , if not, it will create a table with the same structure of the original table
2. Check, whether the structure of both original and archiving table is same, if not, it will rename the old archiving table and create a new archiving table
3. Check for, any identity column exists in the table. This will automatically identify the identity column name
4. Change the identity feature of the column of archiving table. You need to remove the identity property, otherwise, the original table data cannot be inserted as is in archiving table
5. Insert the data from original table into archiving table based on some condition
This stored procedure can be run as a SQL job and keep on running on a specified interval.
However, some enhancement in some functionality will help the procedure to its 100% fail proof, like
1. As of now, to remove the identity feature (not the column), this will remove the identity column and add a column with the same name.
2. Due to this, the original table identify column should be the last one. As add will add a column always last of the structure.
The complete SQL stored procedure
Any suggestions are welcome to improve this..