sql-serverdatabase-administrationsql-server-administration

Detach and Attach a database in Suspect Mode


Problem:

Backups were failing in one of our servers with the below message: (Backups are taken via Netbackup)

Could not allocate space for object 'dbo.backupfile'.'PK__backupfi__57D1800AC4FFEEA3' 
in database 'msdb' because the 'PRIMARY' filegroup is full. Create disk space by deleting 
unneeded files, dropping objects in the filegroup, adding additional files to the 
filegroup, or setting autogrowth on for existing files in the filegroup.>
    DBMS MSG - SQL Message <3009><[Microsoft][ODBC SQL Server Driver][SQL Server]Could not 
insert a backup or restore history/detail record in the msdb database. This may indicate a 
problem with the msdb database.

On checking, I could see that the Mount Drive on which the primary data file of msdb resides, was full (Total size: 99GB, Free space: 0 bytes). The drive didn’t have any unwanted files which I could delete and gain some easy disk space. So I looked around the Drive and found a database log file which was 6GB in size and of which more than 5.5GB was free. I thought shrinking it will free up quite some space in the Drive and solve the problem for the time being. But when I attempted the shrink on the Log file, I got an error message saying ‘It has been marked SUSPECT by recovery’. Fearing the worst, I refreshed the database list in the Object Explorer and Bingo..!! there it was, marked as ‘Suspect’


Solution

  • Solution:

    I checked the other drives in the server and found one with ample free space. So my next attempt was to detach the DB (say ‘XYZ’), move its log file to the other drive and then attach it. This would release enough space in the drive and would also let the DB recover from Suspect mode. So I tried detaching the DB, but it didn’t work.

    Cannot detach a suspect database. It must be repaired or dropped. Cannot be opened as the DB is in Suspect mode
    

    So I tried to bring it to Single User mode: Execute the script to put the DB to single user mode

    USE master;
    GO
    ALTER DATABASE [XYZ]
    SET SINGLE_USER
    WITH ROLLBACK IMMEDIATE;
    GO
    

    Set the database to offline.

    -- Take the Database Offline
    ALTER DATABASE [XYZ] SET OFFLINE WITH
    ROLLBACK IMMEDIATE
    GO
    

    Now I was able to successfully detach the Database. After detaching, I copied the log file to the other drive which had enough free space. Then I tried to attach the DB back. But on attaching, It gave the below error msg:

    Error: 5123 CREATE FILE encountered operating system error 5A(Access denied.)
    

    To resolve it, the following are the steps I did:

    1. Right click on the .mdf file -> Properties
    2. Under the ‘Security’ Tad, press Continue
    3. Click ‘Add’ to include you in ‘Groups or user names’
    4. Give you login Id in the Pop -up that came up, ‘Check Names’ and ‘Ok’
    5. After adding your login, click on it, and in the Box below (Permissions for User), check Allow ‘Full Control’ for the User. It is very important that you Check ‘Full control’ because by default the user will be having only ‘Read’ and ‘Read & Execute’ permissions. And if you try attaching with just those default permissions, you’ll again get Access denied errors.

    After this, I was able to successfully attach the DB, and its status was found to be ‘Normal’.

    Hope this will help somebody :)