Home Page

 

Computers

 

Developer


MSSQL 2000 Database File Manipulation


To remove damaged database from the list of databases stop the SQL server. Move the mdf file with Windows Explorer. Start the SQL server and delete the database in Enterprise Manager.

If a database is detached and can not be attached in the normal way, create a new one with the same name. Stop SQL Server, copy the original database files and start the server.
Now DBCC tools can be run.

Put the database in Emergency mode:

EXEC sp_configure 'allow updates', 1
RECONFIGURE WITH OVERRIDE
GO

BEGIN TRAN

UPDATE master..sysdatabases
	SET status = status | 32768
	WHERE name = 'Lonovi'
IF @@ROWCOUNT = 1
	BEGIN
		COMMIT TRAN
		RAISERROR('emergency mode set', 0, 1)
	END
ELSE
	BEGIN
		ROLLBACK
		RAISERROR('unable to set emergency mode', 16, 1)
	END
GO

EXEC sp_configure 'allow updates', 0
RECONFIGURE WITH OVERRIDE
GO

-- Restart SQL Server at this point.

Now recreate the new log file:

DBCC REBUILD_LOG('Lonovi','e:\SQLData\Lonovi_Log.ldf')

/*
Perform physical and logical integrity checks at this point.
Bcp data out if your integrity checks demonstrate that problems exist.
*/

DBCC CHECKDB
ALTER DATABASE Lonovi SET MULTI_USER
GO

/*
Set database options and recovery model as desired.
*/

ALTER database Lonovi set recovery simple
GO

Then reset database status (if not already):

use master
go
sp_resetstatus 'Lonovi'

/*
Stop and restart SQL Server.
Verify that the database was recovered and is available.
*/

Use Lonovi
DBCC NEWALLOC
Go
DBCC TEXTALL
Go
DBCC CHECKDB
Go

/*
Enable replication: Tools -> Configure... -> Publication Databases, enable.
Recreate replication publications and agents.
Everything should be running fine now.
*/


Some additional helpful commands:

alter database Lonovi remove file Lonovi_data

* Detach database:
sp_detach_db @dbname = 'Lonovi', @skipchecks = TRUE

DBCC SHRINKFILE ('Lonovi_data', EMPTYFILE )

alter database Lonovi add log file (name='Lonovi_data', filename='e:\sqldata\Lonovi_Log.ldf')

sp_attach_single_file_db 'Lonovi', 'e:\SQLData\Lonovi_Data.mdf'