Rename mdf and ldf files ?

  • 1. how do i rename mdf and ldf file names of a database without restoring or creating new database and move the data ?

    2. just as we query "

    select name from master.dbo.sysdatabases

    " for db names is there a way we can list out mdf or ldf file names ?

  • Tara-1044200 (10/15/2009)


    1. how do i rename mdf and ldf file names of a database without restoring or creating new database and move the data ?

    Take the database OFFLINE. Go and change the MDF & LDF files name in it's locations and the take the DB online.

    for db names is there a way we can list out mdf or ldf file names ?

    Try

    select name, filename from master.dbo.sysdatabases

  • San-847017 (10/15/2009)


    Take the database OFFLINE. Go and change the MDF & LDF files name in it's locations and the take the DB online.

    Do just that and the DB will be suspect when brought back online.

    Step 1: Run ALTER DATABASE and use the MOVE option to change the names of the mdf and ldf that is stored in SQL's data catalog

    Step 2: Take the DB offline

    Step 3: Rename the files themselves

    Step 4: Bring the database online

    If you leave out either step 1 or step 3, the DB will be suspect when it's brought online.

    2. just as we query "

    select name from master.dbo.sysdatabases

    " for db names is there a way we can list out mdf or ldf file names ?

    SELECT * FROM sys.master_files

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • San-847017 (10/15/2009)


    Tara-1044200 (10/15/2009)


    1. how do i rename mdf and ldf file names of a database without restoring or creating new database and move the data ?

    Take the database OFFLINE. Go and change the MDF & LDF files name in it's locations and the take the DB online.

    for db names is there a way we can list out mdf or ldf file names ? [/quote]

    make sure you use alter database modify file command between offline and online so SQL knows the new name of the file. Otherwise database won't come back online.

    no 2,

    select d.name,f.name as logical_name,f.physical_name from sys.master_files f join sys.databases d on f.database_id = d.database_id

    ---------------------------------------------------------------------

  • one of these days Gail I will get my reply in just BEFORE you. 🙂

    ---------------------------------------------------------------------

  • george sibbald-364359 (10/15/2009)


    one of these days Gail I will get my reply in just BEFORE you. 🙂

    Type faster 😉

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (10/15/2009)


    george sibbald-364359 (10/15/2009)


    one of these days Gail I will get my reply in just BEFORE you. 🙂

    Type faster 😉

    thats what you said last time. Obviously I have failed to improve. :crying:

    ---------------------------------------------------------------------

  • make sure you use alter database modify file command between offline and online so SQL knows the new name of the file. Otherwise database won't come back online.

    Sorry !...Missed this step.

    Thanks for correcting.

  • If you leave out either step 1 or step 3, the DB will be suspect when it's brought online.

    Gila, Infact it's not coming online at all. Getting the error saying "Unable to open physical file...."

  • San-847017 (10/15/2009)


    If you leave out either step 1 or step 3, the DB will be suspect when it's brought online.

    Gila, Infact it's not coming online at all. Getting the error saying "Unable to open physical file...."

    Correct, it cannot come online.

    It doesn't leave the DB in the offline state, doesn't put it online. In fact (if you check the sys.databases view) the state of a DB after doing this is RECOVERY_PENDING. Not actually the SUSPECT state, but it means almost the same thing.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • It doesn't leave the DB in the offline state, doesn't put it online. In fact (if you check the sys.databases view) the state of a DB after doing this is RECOVERY_PENDING

    Yes.. It is. Thanks Gila.

  • Hi,

    I have tried the steps you suggest (and many other options found elsewhere on the web) but have not been able to get any to work. I first followed the guide at the "Moving TempDB - SQL School Video" on this site and then tried your steps.

    I either end up with the db in "Recovery Pending" state, or the "Bring Online" fails saying that the "Set Offline failed" even it did not say so when I did it! The mdf/ldf is left unchanged and the only way I have found to recover the situation is to delete the db from within SSMS and the windows folder then copy my backup mdf back in, reattach and then I am back where I started with the un-renamed mdf...

    I am a newbie with SQL and am struggling to understand why such a simple sounding task should be so hard (notwithstanding it may be nothing to do with SSMS and I just don't know why)... I have also looked at the file permissions before during after each step and can't see anything changing there (always stay at full permission for all

  • I suggest that you post on a new thread.

    People hardly monitor old threads like this one.

    -- Gianluca Sartori

  • Hey,

    Thanks for the tip and for noticing a post that no one is likely to monitor. FYI and for anyone else who has a similar problem I managed to get it working by following the suggestion about backing up the db and then restoring it - you get the option to rename the mdf/ldf during the restore and can also rename the logical names after the restore via db properties... 🙂

  • another newbie (10/23/2014)


    I managed to get it working by following the suggestion about backing up the db and then restoring it - you get the option to rename the mdf/ldf during the restore and can also rename the logical names after the restore via db properties... 🙂

    Not the best way to do it, totally unnecessary use of resources.

    To rename the files use ALTER DATABASE ... MODIFY FILE ...

    Once you have sent the alter commands to the database take it offline, again using ALTER DATABASE ... SET OFFLINE

    Rename the OS files to match the command you sent to the database, then bring the database online using ALTER DATABASE ... SET ONLINE

    Works flawlessly 😉

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply