简体   繁体   English

撤消对存储过程的更改

[英]undo changes to a stored procedure

I altered a stored procedure and unknowingly overwrote some changes that were made to it by another developer.我更改了一个存储过程,并在不知不觉中覆盖了其他开发人员对其所做的一些更改。 Is there a way to undo the changes and get the old script back?有没有办法撤消更改并恢复旧脚本?

Unfortunately I do not have a backup of that database, so that option is ruled out.不幸的是,我没有该数据库的备份,因此排除了该选项。

The answer is YES , you can get it back, but it's not easy.答案是肯定的,你可以找回来,但这并不容易。 All databases log every change made to it.所有数据库都会记录对其所做的每一次更改。 You need to:你需要:

  1. Shutdown the server (or at least put it into read-only mode)关闭服务器(或至少将其置于只读模式)
  2. Take a full back up of the server对服务器进行完整备份
  3. Get a copy of all the db log files going back to before when the accident happened获取事故发生前所有数据库日志文件的副本
  4. Restore the back up onto another server将备份恢复到另一台服务器
  5. Using db admin tools, roll back through the log files until you "undo" the accident使用 db 管理工具,回滚日志文件,直到您“撤消”事故
  6. Examine the restored code in the stored proc and code it back into your current version检查存储过程中恢复的代码并将其编码回您的当前版本

And most importantly: GET YOUR STORED PROCEDURE CODE UNDER SOURCE CONTROL最重要的是:让您的存储过程代码处于源代码控制之下

Most people don't "get" this concept: You can only make changes to a database;大多数人不“明白”这个概念:您只能对数据库进行更改 you can't roll back the code version like you can with application code.您不能像使用应用程序代码那样回滚代码版本。 To "roll back", you must make more changes and drop/define your stored proc (or whatever).要“回滚”,您必须进行更多更改并删除/定义您的存储过程(或其他)。

Note to nitpickers: By "roll back" I do not mean "transaction roll back".挑剔者注意:“回滚”并不是指“事务回滚”。 I mean you've made your changes and decide one the server is back up that the change is no good.我的意思是您已经进行了更改,并决定备份服务器,因为更改没有好处。

In addition to the sound advice to either use a backup or recover from source control (and if you're doing neither of those things, you need to start), you could also consider getting SSMS Tools Pack from @MladenPrajdic .除了使用备份或从源代码控制恢复的合理建议(如果您没有做这些事情,则需要开始),您还可以考虑从@MladenPrajdic获取SSMS 工具包 His Management Studio add-in allows you to keep a running history of all the queries you've worked on or executed, so it is very easy to go back in time and see previous versions.他的 Management Studio 插件允许您保留您处理或执行的所有查询的运行历史记录,因此很容易及时返回并查看以前的版本。 While that doesn't help you if someone else worked on the last known good version, if your entire team is using it, anyone can go back and see any version that was executed.如果其他人在最后一个已知的好版本上工作,这对您没有帮助,但如果您的整个团队都在使用它,任何人都可以 go 返回并查看已执行的任何版本。 You can dictate where it is saved (to your own file system, a network share, or a database), and fine-tune how often auto-save kicks in. Really priceless functionality, especially if you're lazy about backups and/or source control (though again, I stress, you should be doing these things before you touch your production server again).您可以指定它的保存位置(到您自己的文件系统、网络共享或数据库),并微调自动保存启动的频率。真正无价的功能,尤其是如果您懒于备份和/或源代码控制(尽管我再次强调,你应该在再次接触生产服务器之前做这些事情)。

"Is there a way to undo the changes and get the old script back?" “有没有办法撤消更改并恢复旧脚本?”

Short answer: Nope.简短的回答:不。

:-( :-(

You won't be able to get it back from the database side of things.您将无法从数据库方面取回它。 Your options at this point are pretty much limited to 1) recover from backup, 2) go to source control or 3) hope that someone else has a copy still up in an editor somewhere or saved to a file.此时您的选择几乎仅限于 1) 从备份中恢复,2) go 到源代码控制或 3) 希望其他人在某处的编辑器中仍有副本或保存到文件中。

If neither of these are an option for you, then here's the obligatory "you should take regular backups and use source control"....如果这些都不适合您,那么这是强制性的“您应该定期备份并使用源代码控制”......

You could look through the cached execution plans and try to find the one where your colleague made his changes and run the relevant parts again.您可以查看缓存的执行计划并尝试找到您的同事进行更改的执行计划并再次运行相关部分。

EDIT编辑

Although Bohemian looks to have a good answer if you've got the changes in the TL, this is what I'm talking about.尽管如果您在 TL 中进行了更改,Bohemian 看起来会有一个很好的答案,但这就是我要说的。 Review the SQL text for the plan.查看计划的 SQL 文本。

SELECT  cached.*,
               sqltext.*
         FROM  sys.dm_exec_cached_plans cached
  CROSS APPLY  sys.dm_exec_sql_text (cached.plan_handle) AS sqltext

But as squillman points out, there is no execution plan for DDL.但正如 squillman 所指出的,没有 DDL 的执行计划。

I'm way late to the game on this but I did this same thing this morning and found I had forgot to save my script at some point in the past and needed to recover it.我在这个游戏上迟到了,但我今天早上做了同样的事情,发现我在过去的某个时候忘记了保存我的脚本,需要恢复它。 (It will be in source control after I get done fixing this!!!) (我完成修复后它将在源代码管理中!!!)

Some people mentioned restoring from a backup but no one really mentioned how easy this is if you have a back up.有些人提到从备份中恢复,但没有人真正提到如果你有备份是多么容易。 Moreover, you aren't locked into rolling back the production database.此外,您不会被锁定回滚生产数据库。 I think this is key and assuming you have a back up I would say this is a much better alternative to what has been voted up to the best answer.我认为这是关键,假设你有一个备份,我会说这是一个更好的选择,而不是被投票选出的最佳答案。

All you have to do is take your back up and restore it to a new database.您所要做的就是备份并将其还原到新数据库。 Pull out the sp you are looking for and voila, you've recovered the missing code.拉出你正在寻找的 sp,瞧,你已经恢复了丢失的代码。

Don't forget to drop the newly created database after you've recovered the missing file.恢复丢失的文件后,不要忘记删除新创建的数据库。

I had the same problem, and I don't have the confidence to go restoring from log files to another server.我有同样的问题,我没有信心 go 从日志文件恢复到另一台服务器。 I was pretty distraught until I realised the solution was very simple...我非常心烦意乱,直到我意识到解决方案非常简单......

Press Ctrl-Z over and over until I had undone my changes and the run the ALTER PROCEDURE again.反复按 Ctrl-Z 直到我撤消更改并再次运行 ALTER PROCEDURE。

Admittedly I was pretty lucky that I still had it there to revert to but it really is the easiest fix.诚然,我很幸运我仍然可以恢复它,但它确实是最简单的解决方法。 Probably a bit late now though.不过现在可能有点晚了。

If you have scripted the stored procedure out from management studio object explorer this will work.如果您从管理工作室 object 资源管理器中编写了存储过程的脚本,这将起作用。 Before expand and collapse the object explorer just scroll and point to the stored procedure you have opened.在展开和折叠 object 资源管理器之前,只需滚动并指向您打开的存储过程。 Script the stored procedure as create or alter to then you can get the previous version of the proc since the object explorer doesn't refreshed yet.将存储过程编写为 create 或 alter to 脚本,然后您可以获得以前版本的 proc,因为 object 资源管理器尚未刷新。 This is always my life saver.这永远是我的救命稻草。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM