撤消对存储过程的更改
我更改了一个存储过程,并在不知不觉中覆盖了其他开发人员对其所做的一些更改。有没有办法撤消更改并恢复旧脚本?
不幸的是,我没有该数据库的备份,因此排除了该选项。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我更改了一个存储过程,并在不知不觉中覆盖了其他开发人员对其所做的一些更改。有没有办法撤消更改并恢复旧脚本?
不幸的是,我没有该数据库的备份,因此排除了该选项。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
答案是是,您可以取回它,但这并不容易。所有数据库都会记录对其所做的每一项更改。您需要:
版本 最重要的是:在源代码下获取存储过程代码控制
许多人没有理解这个概念:您只能对数据库进行更改;您无法像使用应用程序代码那样通过将文件替换为以前的版本来回滚存储过程版本。要“回滚”,您必须进行更多更改来删除/定义您的存储过程。
挑剔者请注意:我所说的“回滚”并不是指“事务回滚”。我的意思是,您已经进行了更改,并在服务器恢复后决定该更改没有好处。
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:
And most importantly: GET YOUR STORED PROCEDURE CODE UNDER SOURCE CONTROL
Many people don't grok this concept: You can only make changes to a database; you can't roll back the stored proc version like you can with application code by replacing files with their previous versions. To "roll back", you must make more changes that drop/define your stored proc.
Note to nitpickers: By "roll back" I do not mean "transaction roll back". I mean you've made your changes and decide once the server is back up that the change is no good.
“有没有办法撤消更改并恢复旧脚本?”
简短回答:不。
:-(
"Is there a way to undo the changes and get the old script back?"
Short answer: Nope.
:-(
除了使用备份或从源代码管理恢复的合理建议(如果您没有执行这些操作,则需要开始),您还可以考虑获取 SSMS 工具包,来自 @MladenPrajdic。他的 Management Studio 插件允许您保留您已处理或执行的所有查询的运行历史记录,因此很容易返回过去并查看以前的版本。虽然如果其他人使用了最后一个已知的良好版本,这对您没有帮助,但如果您的整个团队都在使用它,则任何人都可以返回并查看已执行的任何版本。您可以指定它的保存位置(保存到您自己的文件系统、网络共享或数据库),并微调自动保存启动的频率。真正无价的功能,特别是如果您懒于备份和/或源代码控制(尽管我再次强调,您应该在再次接触生产服务器之前执行这些操作)。
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. 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. 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. 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).
您可以查看缓存的执行计划,并尝试找到您的同事进行更改的执行计划,然后再次运行相关部分。
编辑
虽然 Bohemian 看起来有一个很好的答案,如果您在 TL 中进行了更改,这就是我所说的。查看计划的 SQL 文本。
但正如斯奎尔曼指出的那样,DDL 没有执行计划。
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. Review the SQL text for the plan.
But as squillman points out, there is no execution plan for DDL.
您将无法从数据库方面取回它。此时您的选择几乎仅限于 1) 从备份中恢复,2) 转到源代码管理或 3) 希望其他人在编辑器中的某处仍保留有副本或保存到文件中。
如果这些都不适合您,那么这里是强制性的“您应该定期备份并使用源代码管理”......
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.
If neither of these are an option for you, then here's the obligatory "you should take regular backups and use source control"....
我在这方面已经迟到了,但今天早上我做了同样的事情,发现我忘记在过去的某个时刻保存我的脚本,需要恢复它。 (在我修复完这个问题后,它将进入源代码管理!!!)
有些人提到从备份恢复,但没有人真正提到如果您有备份,这将是多么容易。此外,您不会被锁定回滚生产数据库。我认为这是关键,假设您有支持,我会说这是比已投票选出最佳答案更好的替代方案。
您所要做的就是备份并将其恢复到新数据库。拿出你正在寻找的 sp,瞧,你已经恢复了丢失的代码。
恢复丢失的文件后,不要忘记删除新创建的数据库。
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.
Don't forget to drop the newly created database after you've recovered the missing file.
我遇到了同样的问题,并且我没有信心从日志文件恢复到另一台服务器。我非常心烦意乱,直到我意识到解决方案非常简单......
一遍又一遍地按 Ctrl-Z 直到我撤消更改并再次运行 ALTER PROCEDURE。
不可否认,我很幸运,我仍然可以恢复它,但这确实是最简单的修复方法。不过现在可能有点晚了。
I had the same problem, and I don't have the confidence to go restoring from log files to another server. 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.
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.
如果您已从 Management Studio 对象资源管理器编写了存储过程的脚本,那么这将起作用。
在展开和折叠对象资源管理器之前,只需滚动并指向已打开的存储过程。将存储过程编写为 create 或 alter 脚本,然后您可以获得该过程的先前版本,因为对象资源管理器尚未刷新。这永远是我的救星。
If you have scripted the stored procedure out from management studio object explorer this will work.
Before expand and collapse the object explorer just scroll and point to the stored procedure you have opened. 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. This is always my life saver.