Sql-server – Using “Generate Scripts” with EMERGENCY/SINGLE USER mode

recoveryrestoresql serversql-server-2005

I am trying to create an INSERT script to salvage data from a single table in a corrupt database.

I've succesfully put the database online and put it in EMERGENCY / SINGLE USER mode (following this http://www.sqlskills.com/blogs/paul/disaster-recovery-101-hack-attach-a-damaged-database/
)

This allow SELECTs against the data. However, when trying to use the "Generate Scripts" tool in SSMS, I get an error because there already is a connection to the database. I assume the script tool is attempting to create a seconday (sql server local?) connection/open the database to do its work, but my SSMS session is blocking.

Is it possible to ensure that the script tool is using same connection as the already established? Or drop the connection, but keep the database in EMERGENCY mode?

Best Answer

You can drop your connection and the database will stay in EMERGENCY/SINGLE user mode. And the object manager in SSMS does use it's own connection. I'm not sure if the script generator tool uses yet another connection or not. However there is no way to guarantee that your generate scripts tool connection will be the one that takes up the one connection once you have dropped it. The best bet is to generate your script on an alternate database (where ever you are moving your data to should work). Then copy that script to the query window that is connected to the database and run it.

EDIT:

Ok I did a little testing. If you drop your query connection and open up a connection in Object Explorer you can script an INSERT statement (assuming that is what you want).

In Object Explorer find your table, then right click on it. Go to Script Table As -> INSERT To -> Clipboard

enter image description here

Make sure you use clipboard. If you do new query window it will fail. Then disconnect Object Explorer and open a query window to your database and paste.

Otherwise I'll need more information about what type of INSERT statement you are trying to create.

EDIT2: Based on comments

Assuming that you have your destination database (where you are trying to move the data to) on the same instance (and if it isn't make one at least temporarily) then use the following piece of code.

INSERT INTO DestinationDB.dbo.DestinationTable
SELECT * FROM SourceTable

If you have an identity column then you will have to do the following

SET IDENTITY_INSERT DestinationDB.dbo.DestinationTable ON

INSERT INTO DestinationDB.dbo.DestinationTable
(Field List)
SELECT [Field List] FROM SourceTable

SET IDENTITY_INSERT DestinationDB.dbo.DestinationTable OFF