Even with all precautions taken, mishaps with databases are still a possibility. Although it is good to be prepared for a disaster - the database is in the full recovery model and transaction log and database backups are created regularly, there are still some situations when a problem cannot be solved using an out-of-the-box solution.
One of these issues is restoring a single object. It’s not unusual to delete a SQL database object (e.g. a stored procedure) that is still needed, or modify it when the old version might be required later.
How to restore that stored procedure without too much trouble
Version control systems provide just the functionality you need. But this is inapplicable if there is no object already versioned and all that is available is a database backup.
Restoring the full database backup
A full database backup is available with the object that is needed, but it cannot be restored over the existing database, as all other changes made after the full database backup was created, will be overwritten.
The solution is to:
The downside of this method is that, for huge databases, restoring a database takes up a lot of time and space, so this method is very inefficient
Recovering from cache
If the stored procedure has been executed recently, there is a chance it’s still in the cache
SELECT Cached.refcounts, Cached.usecounts, Cached.objtype, SQLText.dbid, SQLText.objectid, SQLText.text, Query.query_plan FROM sys.dm_exec_cached_plans Cached CROSS APPLY sys.dm_exec_sql_text(Cached.plan_handle)SQLText CROSS APPLY sys.dm_exec_query_plan(Cached.plan_handle)Query
The advantage of this method is that you don’t even need a full database backup. The downside is that if the stored procedure hasn’t been executed recently, and it will not be possible to recover the stored procedure.
ApexSQL Diff is a SQL Server database comparison and synchronization tool which detects differences between database objects and resolves them without errors. It has granular filtering that enables comparing only specific objects. As it reads database backups, you can compare them without restoring first
Click New in the Project management dialog
Select Use filter for Procedures
Uncheck all stored procedures, except the one that is to be recovered
Select the stored procedure that is to be recovered in the Main grid
On the Home tab, in the Actions group, click Synchronize

Review the synchronization summary and warnings, if there are any, and click Next
In the Output options step of the Synchronization wizard, select Create a synchronization script
Check out the script and press F5 to execute it
Once the stored procedure is recreated, you’ll get the following message:
If a stored procedure is lost or modified, there’s no need to restore the whole database from a backup or search through the cache. Use ApexSQL Diff to create a DDL script directly from the database backup only for the objects that are to be restored.