SQL Server – Fix – Error: 17204 and FCB::Open failed

1. First error in Windows Application log file
FCB::Open failed: Could not open file D:Program FilesMicrosoft SQL ServerMSSQL11.MSSQLSERVERMSSQLDATAMyDB.mdf for file number 1. OS error: 32(The process cannot access the file because it is being used by another process.).

2. Second error.
Error: 17204, Severity: 16, State: 1.

The fix (for me)

ALTER DATABASE MyDB SET ONLINE

See also this.
And this and this command “alter database set emergency dbcc checkdb (repair_allow_data_loss)”.

SQL SERVER – Fix: Error: 15138 – The database principal owns a schema in the database, and cannot be dropped

Script to list all the schemas for a database and then construct the code to change it to [dbo].

See here for more information.

USE MyDatabase
go

-- Get the associated schema
SELECT  *
FROM    information_schema.schemata

DECLARE @vcUser VARCHAR(128),
    @vcSQL VARCHAR(8000)
SELECT  @vcUser = 'any non dbo username causing the problem'

IF EXISTS ( SELECT  s.name
            FROM    sys.schemas s
            WHERE   s.principal_id = USER_ID(@vcUser) )

-- Now replace the result name in following script:
    SELECT  @vcSQL = 'ALTER AUTHORIZATION ON SCHEMA::' + QUOTENAME(s.name) + ' TO dbo;'
    FROM    sys.schemas s
    WHERE   s.principal_id = USER_ID(@vcUser)
            
PRINT @vcSQL
IF 1 = 1
    EXEC (@vcSQL)
    

-- Get the associated schema
SELECT  *
FROM    information_schema.schemata