Small project database, with old backup, ended up in corrupt state. This happened. A corrupted database; all-in-all 150MB. I was super annoyed. Let alone, I had some R and Python code there and some – yes 🙂 – predictive models, I forgot to have automated backup created. Data stored in database should not be a problem, but the table with last stored model was the information / row, I needed
So the problem with serialized models stored in a table is, that it needs to be fully compatible and correctly restored to the last bit in order for R / Python to be read correctly. So missing one bit, and the model is literally useless.
My models are stored in T-SQL Table:
SELECT [model_name] ,[model] ,[accuracy] ,[model_date] ,[version_Model] ,[Approved_by] FROM [SQLR].[dbo].[Predictive_models]
Once I calmed my self down, I started with my options. Worse case scenario, I can restore two-months old backup, but what are my other options?
Using the Microsoft built-in Database Console Command
utility. The utility comes with multiple repair options to resolve corruption errors in MS SQL database. But, yeah, not that it is a super challenging but I started to repair with option:
DBCC CHECKDB REPAIR_ALLOW_DATA_LOSS
and it ended with data loss. Hurray for the better restore. But still was not successful enough due to the data loss.
Digging further, I used PowerShell Restore-SqlDatabase cmdlet for restoring SQL Server database. But, was left empty-handed, since this works only with when you have a restorable backup.
The benefits using Stellar software repair for MS SQL Server:
- Comes with a simple to use GUI which makes the repair process straightforward.
- Helps repair SQL database quickly in three simple steps: 1. Select database file. 2. Repair database file. 3. Preview and Save repaired file.
- Recovers all major database components of SQL server including tables, views, indexes, stored procedures, etc.
- Resolves corruption errors in databases running on MS SQL Server 2019, 2017, 2016, and older versions
- Repairs SQL tables with PAGE and ROW compression.
- Allows saving the repaired file in MS SQL, HTML, XLS, and CSV formats
- Provides Windows and SQL authentication options to connect to the server
- Supports Standard Compression Scheme for Unicode (SCSU) for SQL Server 2008 R2.
(based on the info/sales text from their website)
Step-by-step to repair corrupt .mdf file with Stellar Repair for MS SQL Server
After downloading, stop the MS SQL Server service and copy database (mdf. file) to different location.
1. The Instruction window is displayed. Click OK.
2. Select Database window appears. Click Browse to select .mdf file you want to repair.
Great feature is also, the ability to recover deleted database records; in this case select ‘Include Deleted Records’ checkbox (and make sure that you have the appropriate database recovery model selected).
After selecting the desired file, click repair to engage the process. Hopefully only one mdf file is corrupt 🙂 But you can also select multiple.
There is also the ability to select the SQL Server version if the software is not able to detect the MSSQL Server database file version. In this case, just select the version of the SQL Server where your database was originally created.
After the process completes with recovery, you can inspect what has been recovered by Stellar.
And I can recover only selected table and prior to recovering / saving, I can also inspect the content of my data in right pane. Since the models are serialized and stored as blob, I can only hope that R will be able to de-serialized it and used it again using sp_execute_external_script.
Save the data back to MDF file and later attach it to the SQL Server or in my case, exporting to CSV file is also a possibility since the table was not that big.
After attaching back new MDF file with table holding the models:
i’m able to run successfully the R script and score new values.
After all, I am happy that I found this software that helped me dealing with corrupt table / database and I can score the data on my older model that has great accuracy.
As always, happy coding!