恢复未保存的 SQL 查询脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15034832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:51:22  来源:igfitidea点击:

Recover unsaved SQL query scripts

sqlsql-servertsqlsql-server-2012ssms

提问by BumbleBee

How to recover the unsaved scripts if the SSMS crashes / unsaved tab gets accidentally closed?

如果 SSMS 崩溃/未保存的选项卡意外关闭,如何恢复未保存的脚本?

回答by BumbleBee

Posting this in case if somebody stumbles into same problem.

如果有人偶然发现同样的问题,请发布此信息。

Googled for Retrieve unsaved Scriptsand found a solution.

谷歌搜索检索未保存的脚本并找到了解决方案。

Run the following selectscript. It provides a list of scripts and its time of execution in the last 24 hours. This will be helpful to retrieve the scripts, if we close our query window in SQL Server management studio without saving the script. It works for all executed scripts not only a view or procedure.

运行以下select脚本。它提供了一个脚本列表及其在过去 24 小时内的执行时间。如果我们在不保存脚本的情况下关闭 SQL Server 管理工作室中的查询窗口,这将有助于检索脚本。它不仅适用于视图或过程,还适用于所有已执行的脚本。

Use <database>
SELECT execquery.last_execution_time AS [Date Time], execsql.text AS [Script] FROM sys.dm_exec_query_stats AS execquery
CROSS APPLY sys.dm_exec_sql_text(execquery.sql_handle) AS execsql
ORDER BY execquery.last_execution_time DESC

回答by Matt

You maybe able to find them in one of these locations (depending on the version of Windows you are using).

可以在这些位置之一找到它们(取决于您使用的 Windows 版本)。

Windows XP

视窗 XP

C:\Documents and Settings\YourUsername\My Documents\SQL Server Management Studio\Backup Files\

Windows Vista/7/10

视窗 Vista/7/10

%USERPROFILE%\Documents\SQL Server Management Studio\Backup Files

OR

或者

%USERPROFILE%\AppData\Local\Temp

Googled from this sourceand this source.

从这个来源这个来源谷歌搜索

回答by DonQ

A bit late to the party, but none of the previously mentioned locations worked for me - for some reason the back up/autorecovery files were saved under VS15 folder on my PC (this is for SQL Server 2016 Management Studio)

聚会有点晚了,但前面提到的位置都不适合我 - 出于某种原因,备份/自动恢复文件保存在我 PC 上的 VS15 文件夹下(这是用于 SQL Server 2016 Management Studio)

C:\Users\YOURUSERNAME\Documents\Visual Studio 2015\Backup Files\Solution1

C:\Users\YOURUSERNAME\Documents\Visual Studio 2015\Backup Files\Solution1

You might want to check your Tools-Options-Environment-Import and Export Settings, the location of the settings files could point you to your back up folder - I would never have looked under the VS15 folder for this.

您可能想要检查您的工具-选项-环境-导入和导出设置,设置文件的位置可能会指向您的备份文件夹 - 我永远不会在 VS15 文件夹下查看此内容。

回答by Mandar

Use the following location where you can find all ~AutoRecover.~vs*.sql(autorecovery files):

使用以下位置可以找到所有~AutoRecover.~vs*.sql自动恢复文件):

C:\Users\<YourUserName>\Documents\SQL Server Management Studio\Backup Files\Solution1

回答by frapeti

Maybe you don't find the suggested directories or your recovery file is missing, thanks god I replicated the crash with an unsaved script and lead me to this directory:

也许你没有找到建议的目录或者你的恢复文件丢失了,感谢上帝我用一个未保存的脚本复制了崩溃并引导我到这个目录:

C:\Users\user\OneDrive\Documents\Visual Studio 2015\Backup Files\Solution1

C:\Users\user\OneDrive\Documents\Visual Studio 2015\Backup Files\Solution1

So, maybe this saves your day :)

所以,也许这可以节省您的一天:)

回答by frustrationmultiplied

I know this is an old thread but for anyone looking to retrieve a script after ssms crashes do the following

我知道这是一个旧线程,但对于希望在 ssms 崩溃后检索脚本的任何人,请执行以下操作

  1. Open Local Disk (C):
  2. Open users Folder
  3. Find the folder relevant for your username and open it
  4. Click the Documents file
  5. Click the Visual Studio folder or click Backup Files Folder if visible
  6. Click the Backup Files Folder
  7. Open Solution1 Folder
  8. Any recovered temporary files will be here. The files will end with vs followed by a number such as vs9E61
  9. Open the files and check for your lost code. Hope that helps. Those exact steps have just worked for me. im using Sql server Express 2017
  1. 打开本地磁盘 (C):
  2. 打开用户文件夹
  3. 找到与您的用户名相关的文件夹并打开它
  4. 单击文档文件
  5. 单击 Visual Studio 文件夹或单击备份文件文件夹(如果可见)
  6. 单击备份文件文件夹
  7. 打开 Solution1 文件夹
  8. 任何恢复的临时文件都会在这里。文件将以 vs 结尾,后跟一个数字,例如 vs9E61
  9. 打开文件并检查丢失的代码。希望有帮助。这些确切的步骤对我有用。我正在使用 Sql server Express 2017

回答by M PRATAP

I am using Windows 8 and found the missing scripts in the path below:

我使用的是 Windows 8 并在以下路径中找到了缺失的脚本:

C:\Users\YourUsername\Documents\SQL Server Management Studio\Backup Files

回答by noobjet

Go to SSMS >> Tools >> Options >> Environment >> AutoRecover

转到 SSMS >> 工具 >> 选项 >> 环境 >> 自动恢复

There are two different settings:

有两种不同的设置:

1) Save AutoRecover Information Every Minutes

1) 每分钟保存一次自动恢复信息

This option will save the SQL Query file at certain interval. Set this option to minimum value possible to avoid loss. If you have set this value to 5, in the worst possible case, you can lose last 5 minutes of the work.

此选项将按特定时间间隔保存 SQL 查询文件。将此选项设置为可能的最小值以避免丢失。如果您将此值设置为 5,在最坏的情况下,您可能会丢失最后 5 分钟的工作。

2) Keep AutoRecover Information for Days

2) 保留自动恢复信息数天

This option will preserve the AutoRecovery information for specified days. Though, I suggest in case of accident open SQL Server Management Studio right away and recover your file. Do not procrastinate this important task for future dates.

此选项将保留指定天数的自动恢复信息。不过,我建议在发生意外时立即打开 SQL Server Management Studio 并恢复您的文件。不要把这个重要的任务拖到未来的日期。

回答by Andrei Rantsevich

SSMSBoost add-in (currently free)

SSMSBoost 插件(目前免费)

  • keeps track on all executed statements (saves them do disk)
  • regulary saves snapshot of SQL Editor contents. You keep history of the modifications of your script. Sometimes "the best" version is not the last and you want to restore the intermediate state.
  • keeps track of opened tabs and allows to restore them after restart. Unsaved tabs are also restored.
  • 跟踪所有已执行的语句(将它们保存在磁盘上)
  • 定期保存 SQL 编辑器内容的快照。您保留脚本修改的历史记录。有时“最好”的版本不是最后一个,您想要恢复中间状态。
  • 跟踪打开的标签并允许在重新启动后恢复它们。未保存的选项卡也会恢复。

+tons of other features. (I am the developer of the add-in)

+大量其他功能。(我是插件的开发者)

回答by Naveen

You can find files here, when you closed SSMS window accidentally

当您不小心关闭 SSMS 窗口时,您可以在这里找到文件

C:\Windows\System32\SQL Server Management Studio\Backup Files\Solution1