Move logship database file

All we need is an easy explanation of the problem, so here it is.

I want to move a ldf file from one drive to another. Some idiot who is definitly not me restored the log file to the wrong drive which is nearly full now.

The database is readonly/standby for a logship environment. Alter database .. modify file does not work since the DB is readonly. I can not detach/attach because i can not specify the standby file with an create .. for attach.

If i use alter database :

Msg 5004, Level 16, State 4, Line 9
To use ALTER DATABASE, the database must be in a writable state in which a checkpoint can be executed.

If i try attach :

Msg 1824, Level 16, State 1, Line 11
Cannot attach a database that was being restored.

The small test case i made :

-- create ro/standby db
restore database test 
    from disk = 'D:\BakMcBakface.bak'
    move 'data' to 'D:\test.mdf',
    move 'log' to 'D:\test.ldf',

-- try alter
ALTER DATABASE test MODIFY FILE ( NAME = 'log', FILENAME = 'd:\test2.ldf' );

-- try detach/attach db
EXEC master.dbo.sp_detach_db @dbname = N'test'

-- attach db
    for attach
    -- with standby='D:\test.standby'

As a last resort i could do a full restore but this is a multi TB database and i try to avoid that. If i could just attach in standby mode i’m done. I could also modify the sys tables directly but i rather not be the cause of the return of Cthulhu.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Rather than the detach/attach, take the database offline, change the location, move the file, and set back online. Example DDL:

--take database offline

--modify the file location
ALTER DATABASE test MODIFY FILE ( NAME = 'log', FILENAME = 'd:\test2.ldf' );

--manually move the file to the new location

--finally, set database online

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply