Newbie - Detach / Attach in SQL 2005

  • Hi all!

    I have no DBA experience, this is very new to me coming from desktop support.

    I would like to move a database from one server to another. I have read up on it (here and other sites) and looks like using detach/attach through the Studio consel would be my best bet.

    It appears most sites recommend this be done through code (TSQL?), but I have no idea even where to begin/invoke this. Others have suggested using backup/restore but this is out of my scope right now. I will be going on course soon but this needs to be done sooner than later.

    From what I understand this should work if I:

    -Stop SQL Agent service on server #1.

    -Detach DB on server #1.

    -Copy .mdf and .ldf files from server #1 to #2.

    -Attach DB on server #2 using the files.

    Could you wizards give me the thumbs up or thumbs down on this or if there is anything else I should do or watch for?

    Thanks!

  • Hi...

    Its not the Agent service, but the SQL Server service should be stopped. But, there are other things need to be considered, like - informing the business users about the downtime, check if there are any other servers accessing this database through linked server configuration, checking if users are mapped properly etc.,

    [font="Verdana"]Renuka__[/font]

  • I would recommend using Backup and Restore. Much easier and if something is incorrect, you still have the original database up and running. Also, how do you plan to transfer the logins? The Users in the database will go with the database, but the mating Logins will not. You will have to either transfer them, or drop them all and re-create them on the other box.

    All of this is in the SQL Books Online. Have you read any of the material there? If gives you syntax for all SQL code that you need. You can use the SSMS Interface as well if you are comfortable there. Both preform the same tasks. Meaning, a backup command in T-SQL will get you the same thing as if you right click and select Backup from the list.

    I would leave the original alone until you get the database on the other box up and running the way you want. That means, using Backup and Restore, or creating an SSMS package to pump everything over. I would also create a full backup of the original just before you begin. You can always restore that if you really need too. But once you have a backup, copy it over to the other box and restore it.

    Andrew SQLDBA

  • Thanks for the quick replies guys. The DB is just in it's infancy, there is only one login so far, so as far as that goes it shouldn't be a problem. I'll double check with the other guy who does the programming of the DB to see if anything else is being linked to it.

    As far as stopping the service, the SQL Server service has to be stopped?

    How about the SQL Server Agent service?

  • Not when you take a backup. Nothing has to be stopped, and no user has to exit. Just create a Full backup and move the backup file to where ever you like.

    Andrew SQLDBA

  • No services need to be stopped to detach and attach a database. Just note that the DB will be unavailable while the copy is in process.

    Backup/restore is a better option, users could carry on using the DB while you're backing it up, no one would notice anything.

    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
  • Opps..My mistake!!! No need to stop any service for detach/attach of user DBs...

    [font="Verdana"]Renuka__[/font]

  • Thanks guys.

    Is there a difference as to how user accounts are transfered over using restore vs. detach/attach, or if they are transfered at all?

  • Since you said that you have only the one account, I would just re-create it by hand. Much more simple for only the one account.

    You will have the original database still up and running if you need to look at any mappings. I would think there are none for only one account. This is most likely the account used in the database connections from the app.

    Andrew SQLDBA

  • Both will transfer the database users. They're stored in the database, hence they move with the database.

    Neither will transfer the login information. That you'll have to script out, preferably with the SID (check Books Online for details) and create on the other server.

    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
  • If you use this method, make sure that users are not entering or modifying data because you will have issues with transactions made to the old database after the backup was completed, but before the new database was restored.

  • As an update we went ahead with the detach/attach, as this is what the DB programmer suggested in the end for convenience.

    I am still puzzled as to why people are against this method, well I suppose if the DB is in production it causes more room for error than a backup/restore. Since this DB is fairly new and not utilized this didn't pose a problem.

    Thanks everyone for your help!

Viewing 12 posts - 1 through 11 (of 11 total)

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