Recover unsaved SQL query scripts

前端 未结 14 1571
旧巷少年郎
旧巷少年郎 2020-12-07 06:55

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

相关标签:
14条回答
  • 2020-12-07 07:06

    Posting this in case if somebody stumbles into same problem.

    Googled for Retrieve unsaved Scripts and found a solution.

    Run the following select script. 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.

    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
    
    0 讨论(0)
  • 2020-12-07 07:07

    I was able to recover my files from the following location:

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

    There should be different recovery files per tab. I'd say look for the files for the date you lost them.

    0 讨论(0)
  • 2020-12-07 07:07

    I use the free file searching program Everything, search for *.sql files across my C: drive, and then sort by Last Modified, and then browse by the date I think it was probably last executed.

    It usually brings up loads of autorecovery files from a variety of locations. And you don't have to worry where the latest version of SSMS/VS is saving the backup files this version.

    0 讨论(0)
  • 2020-12-07 07:08

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

    C:\Users\YourUsername\Documents\SQL Server Management Studio\Backup Files
    
    0 讨论(0)
  • 2020-12-07 07:12

    SSMSBoost add-in (currently free)

    • 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.

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

    0 讨论(0)
  • 2020-12-07 07:14

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

    C:\Windows\System32\SQL Server Management Studio\Backup Files\Solution1
    
    0 讨论(0)
提交回复
热议问题