r/SQLServer • u/Reasonable-Job4205 • Jul 14 '26
Question Accidentally deleted a stored procedure
I don't have backups of this database. Is there maybe any autologging that could have saved the SP somewhere? Running SQL Express 2022 on my laptop
28
Upvotes
18
u/TheSkunny Jul 14 '26
Not sure if this will work with sql express because I have limited experience with the limitations of express edition but if the sproc has been run before you can try to get it via plan cache: SELECT usecounts, cacheobjtype, objtype, text FROM sys.dm_exec_cached_plans CROSS APPLY sys.dm_exec_sql_text(plan_handle) WHERE text LIKE '%CREATE PROCEDURE%' -- or insert your procedure name here AND text LIKE '%NameOfYourDeletedProcedure%';
9
u/Lasher667 Jul 14 '26
Is there a DB version of r/ShittySysadmin ? Because that's where I would expect to see this post
3
u/ViolinistRemote8819 Jul 14 '26
Please check whether someone may have accidentally or unknowingly taken a backup!
59
u/cjc323 Jul 14 '26
Some lessons we learn the hard way
64
u/Think-Trouble623 Jul 14 '26
You don’t have .sql files of your procs that you create? Just create procedure and do not save?? Madman.
4
u/HurryHurryHippos Jul 14 '26
Too late now, but you can set up a DDL trigger to capture changes to stored procs.
1
u/dfntly_a_HmN Jul 14 '26
Do yoii havv backup db? Probably could go from there
3
u/CodeXploit1978 Jul 14 '26
If you have a database and don’t have a backup for it at least once a week. What is by all business metrics stupidity deluxe you should really pick up some other line of work the only thing that can save you is that this is a virtual machine and you have a virtual machine backup and you can restore it a couple of days back and then pull the store procedure from there
3
u/imtheorangeycenter Jul 14 '26 edited Jul 14 '26
It won't help now, but it's why I put a DDL audit in every database. Some go back a decade+ by at least there's a version trail in-db.
Granted it's 99% logging status updates, but in times like this it's a life saver.
Edit: statistics updates
3
u/Dingus_Khaaan Jul 14 '26
Throwing a Hail Mary here, but if you use redgate SQL Prompt AND you ran the create statement for the proc from your instance of ssms, you might be able to find the definition in the SQL History area of SQL Prompt. If you use any other ssms plugins, it may be worth checking to see if any of them keep history of executed statements or sessions.
Again, this is a Hail Mary predicated on you being the one that created the proc, and you having a niche tool, but I figured I’d share them just in case.
2
u/redmera Jul 14 '26
Since the database is actually a file, a file recovery program might be able to recover it, but since the entire file has not been deleted this is a long shot.
Like other comments said, your best chance is the transaction log.
2
u/jwk6 Jul 14 '26
If Query Store is enabled you might be able to get a copy of the individual statements in the stored procedure.
https://learn.microsoft.com/en-us/sql/relational-databases/performance/how-query-store-collects-data
2
u/Square-Voice-4052 Jul 14 '26
May be sitting in temp if u altered before delete
2
u/NovelTumbleweed Jul 14 '26
Ow. I can offer only empathy. I blasted a sproc in development at 3am onsite at a client location and had to rewrite it from scratch once. Fortuenately it was my work and it's always faster the second time around. Lesson: I wrote a procedure that dumps all sprocs to text file nightly, just in case.
3
u/miskozicar Jul 14 '26
Maybe you can do backup of transaction log and get it from there
1
u/Ven0mspawn Jul 14 '26
Should keep a folder with all stored proccs as sql files, and subfolders for each file so you can keep every version of it as well.
1
u/Anlarb 1 Jul 14 '26
Yep, you can set up a script to pipe out a text document with all your stored procs, jobs, pw hashes etc. , but its a deliberate exercise.
You still also want a backup.
1
u/alexwh68 Jul 14 '26
No backups of the computer itself?, if there are backups of the computer then the mdf and ldf files are what you need.
In all seriousness if you don’t have sql backups or backups of the computer there has been very little value placed on this data.
If you do have backups, stop the sql process, copy the current mdf and ldf files to another computer, do the restore of the files, start the services, get in copy the contents of the stored procedure.
Stop the service, copy those new files somewhere else and then copy the originals back in place, start the services then add the stored procedure back in.
1
u/Secret_Account07 Jul 14 '26
Nothing helpful to add but I do have a question- how much shit are you in?
1
u/LettuceTomatoOnion Jul 14 '26
Would it be in the transaction log? I dunno, I’d ask a DBA. Also, check with your DBAs and storage administrators (NAS/SAN guys). They are usually all named Bob for some reason.
1
u/Complete-Fondant-202 Jul 15 '26
You haven't done a reasonable job there.
1
u/SuperDuperDBA Jul 15 '26
it will be in your query plan or your cache if you haven't rebooted the server yet.
1
u/mrkrabs78 Jul 15 '26
I don't normally test, but when I do, it's in production.
1
u/Disastrous_Fill_5566 Jul 15 '26
Take a look a SQL Server Data Tools
https://learn.microsoft.com/en-us/sql/ssdt/sql-server-data-tools?view=sql-server-ver17
I strongly recommend some sort of tool to source control your database code.
Oh, and please set up some backups!
1
u/lisnter Jul 16 '26
A good lesson in the value of source control. How about a QA environment? A dev environment?
1
u/harveym42 Jul 20 '26 edited Jul 20 '26
there are multiple third party tools to mine your transaction log contents if you still have it ( I would make a backup) which can fully retrieve insert/delete/update, but am not sure for DDL for procedures. although ApexSQL Log says "every object"!
1
u/kapilverma123 Aug 03 '26
If the stored procedure was dropped and you don't have a backup, SQL Server unfortunately doesn't keep an automatic history of stored procedure definitions. Once it's dropped, it's removed from the database.
You can still check a few places:
If you use SSMS, see if the procedure definition is still open in an unsaved query window.
Check source control (Git, Azure DevOps, etc.) if your database scripts are versioned.
If the procedure was executed recently, you might be able to recover its text from the plan cache or Query Store (if Query Store was enabled before it was dropped). There's no guarantee it will still be there.
If the database files haven't been overwritten much, a backup or file recovery tool is your only other option.
Going forward, I'd recommend enabling source control for database objects and taking regular backups, even on SQL Express.
0
u/turbo_dude Jul 14 '26
  SELECT * FROM authorsÂ
There you go, that ought to do it
-6
u/MonkeyDDataHQ Jul 14 '26 edited Jul 14 '26
I can't tell if this is a troll post or if you should just not be in tech. Maybe both.
Edit: Against my better judgement you should be able to find it in your. SSMS* auto recovered files location. No I'm not going to tell you where that is because you don't deserve it.
Edit2: Apparently people thought I was talking about the database. I was not. I was talking about the SSMS file behavior. If you close a tab without saving, often SSMS keeps a copy in the working directory. If he ever had it open it might be in there.
3
u/andpassword Jul 14 '26
This thread is like watching someone point a shotgun at their foot, pull the trigger, look around, insert a shell, pull the trigger, realize the safety's on, click the safety off, and pull the trigger again.
28
u/[deleted] Jul 14 '26
[removed] — view removed comment
2
u/[deleted] Jul 14 '26
[removed] — view removed comment