Detach and Attach database move filegroup

  • Experts,

    I need your advice with regards to the action i am planning to do during my maintenance activity. I never did this before and need your expert advice.

    I am going to move my 2 mdf and 1 ndf file in my filegroup located in my drive E to other drive partition.

    Please advice if my steps are correct and will not have any lost data in my transaction logs.

    1. Detach mydb

    2. move 2 mdf and 1 ndf file to drive F which has more space.

    3. Attach mydb with the new filegroup drive location which is F:\...

    Please advice if this process will not cause me any lost data in the transaction logs.

    Thanks.

    Arnet

  • I suggest two more things before starting any of that.

    1. Backup the database

    2. Disable access to the server for all connections not related to you, the team, or whomever is working on the project for the duration of the project.

    And then of course, make sure to do it during a maintenance window.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • CirquedeSQLeil (3/17/2010)


    I suggest two more things before starting any of that.

    1. Backup the database

    2. Disable access to the server for all connections not related to you, the team, or whomever is working on the project for the duration of the project.

    And then of course, make sure to do it during a maintenance window.

    Thanks for your suggestion.

    Is this mean that I will not have any data lost after doing this once i did the procedures mentioned?

    Thanks,

    Arnet

  • If you follow all these steps, you should be fine as far as data loss goes.

    In other words, you will have your bases covered and should be protected against data loss.

    I might add one more step to the backup - copy the backup to another server somewhere (thus have two copies). This is an extra precautionary step.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • CirquedeSQLeil (3/17/2010)


    If you follow all these steps, you should be fine as far as data loss goes.

    In other words, you will have your bases covered and should be protected against data loss.

    I might add one more step to the backup - copy the backup to another server somewhere (thus have two copies). This is an extra precautionary step.

    Thanks for your wonderful advice. This will make me comfortable now.

  • You're welcome.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • running the below alter DB restricted user will make sure all transactions have been completed before the detach. Running something like the below will make sure you have as little issue as possible

    USE MASTER

    GO

    BACKUP DATABASE DBName

    TO...

    GO

    ALTER DATABASE DBName SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE

    BACKUP DATABASE DBName

    TO...

    WITH DIFFERENTIAL

    SP_DETACH_DB 'DBName'

    GO

    XP_CMDSHELL 'COPY file1'

    XP_CMDSHELL 'COPY file2'

    XP_CMDSHELL 'COPY file3'

    GO

    SP_Attach_DB 'DBName', 'File1', ...

    GO

    ALTER DATABASE DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE

    GO

  • You cannot lose data in a detach/attach. SQL Server supports the ACID requirements for relational databases. D stands for durability, meaning once a transaction has committed, the changes that transaction made are durable and will not be lost.

    Sure, take a backup in case something goes wrong and tell the users that the app won't be available for the period, but that's just good common sense.

    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
  • JDixon-385826 (3/17/2010)


    running the below alter DB restricted user will make sure all transactions have been completed before the detach. Running something like the below will make sure you have as little issue as possible

    USE MASTER

    GO

    BACKUP DATABASE DBName

    TO...

    GO

    ALTER DATABASE DBName SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE

    BACKUP DATABASE DBName

    TO...

    WITH DIFFERENTIAL

    SP_DETACH_DB 'DBName'

    GO

    XP_CMDSHELL 'COPY file1'

    XP_CMDSHELL 'COPY file2'

    XP_CMDSHELL 'COPY file3'

    GO

    SP_Attach_DB 'DBName', 'File1', ...

    GO

    ALTER DATABASE DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE

    GO

    I would exclude this step, won't make any difference.

    BACKUP DATABASE DBName

    TO...

    WITH DIFFERENTIAL

    EnjoY!
  • well I agree you do not need the diff backup but if you want to be 100% forsure you covered all basis I would keep it:-D

  • GTR (3/18/2010)


    JDixon-385826 (3/17/2010)


    running the below alter DB restricted user will make sure all transactions have been completed before the detach. Running something like the below will make sure you have as little issue as possible

    USE MASTER

    GO

    BACKUP DATABASE DBName

    TO...

    GO

    ALTER DATABASE DBName SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE

    BACKUP DATABASE DBName

    TO...

    WITH DIFFERENTIAL

    SP_DETACH_DB 'DBName'

    GO

    XP_CMDSHELL 'COPY file1'

    XP_CMDSHELL 'COPY file2'

    XP_CMDSHELL 'COPY file3'

    GO

    SP_Attach_DB 'DBName', 'File1', ...

    GO

    ALTER DATABASE DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE

    GO

    I would exclude this step, won't make any difference.

    BACKUP DATABASE DBName

    TO...

    WITH DIFFERENTIAL

    Thanks for your comments.

    By the way, i am simulating in my test environment. I am using

    exec sp_detach_db 'MYDB', false

    but it took so long more than 1 hour.

    Is it ok to use exec sp_detach_db 'MYDB' command only not to update the statistics then move my filegroup to other location and attach?

    What would be the difference and consequences if i did exec sp_detach_db 'MYDB' comand only?

    Thanks in Advance to all of you.

  • Small point: sp_attach_db is deprecated and will be removed in a future version. Use CREATE DATABASE...FOR ATTACH instead.

    There is a good SSC article on this topic by MVP Jonathan Kehayias: http://www.sqlservercentral.com/articles/Administration/65896/

    Paul

  • Paul White NZ (3/20/2010)


    Small point: sp_attach_db is deprecated and will be removed in a future version. Use CREATE DATABASE...FOR ATTACH instead.

    There is a good SSC article on this topic by MVP Jonathan Kehayias: http://www.sqlservercentral.com/articles/Administration/65896/

    Paul

    Hi,

    Is this applicable also in SQL Server 2000? I will do this activity in SQL server 2000. Will I face any problem later on if I use detach and attach in SQL 2K server?

    Thanks.

  • Hi.

    Just one last question and i know it is critical.

    How about the user permission and security? What will happen when i use detach and attach database in my SQL 2000?

    Do i need to execute some tsql to return to it's original mode or no need and the database will be back to normal?

    Please help.

    Thanks.

  • There will be one small issue you may need to verify, any logins whose default database is MyDB (the one you are moving via detach/attach) will probably need fixing after you attech the database in its new location.

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

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