![]() |
|
|
By: Mohammed Moinudheen | Read Comments (4) | Print Mohammed is a SQL Server DBA with over 6 years experience managing production databases for a few Fortune 500 companies. Related Tips: 1 | 2 | 3 | 4 | 5 | More |
|
I am running out of disk space for one of my SQL Server replicated databases. I would like to move the database files of my publication database, which is part of transactional replication, to a drive on the same server, which is has more free space. Which is the better option for moving the files without impacting the transactional replication: sp_attach_db/sp_detach_db or alter database modify file? Check out this tip for the solution.
Running out of disk space is an all too common issue. Databases grow over time and we need to move them to support the business. In this tip will review two options (sp_attach_db/sp_detach_db and alter database modify file) to see which one makes most sense.
The sp_detach_db option is a deprecated feature and will not be available in future SQL Server versions. However, we can try this command to see what happens. When we try this option on a replication publication database we get the following error message as shown below:
Msg 3724, Level 16, State 1, Line 1 Cannot drop the database '%database name%' because it is being used for replication.
In order to perform the detach operation we need to disable the publishing option using the sp_replicationdboption stored procedure. Per Books Online, if this stored procedure does not work, we need to use the sp_removedbreplication stored procedure, which removes all the replication objects in the database.
In order to overcome shortcomings from option 1, and move the publisher's database files, we could use the alter database modify file command.
In this example, we will attempt to move the log file of a replication publication database r_pub. As a first step, we will get the information about the publisher database using the sp_helpdb system stored procedure. We need to collect the file name details so that we can use them in the alter database command. Below is an example of the data from sp_helpdb:
Based on the information above, we can run the following command in SQL Server Management Studio:
ALTER DATABASE r_pub
MODIFY FILE (NAME = r_pub_Log,
FILENAME = 'Enter valid path of drive location\r_pub_Log.ldf');
After this, open SQL Server Management Studio and navigate to Instance | SQL Server Agent | Job Activity Monitor and stop the replication log reader agent job and distribution agent job as shown below:
We stop these jobs because we do not want the log reader agent to connect to the publisher database.
Our next step is to bring the publisher database offline. Use the command below to accomplish this task:
ALTER DATABASE r_pub SET OFFLINE
Once the publisher database is offline, move the log file to the new location in Windows Explorer as specified in the alter database command above.
After the log file is moved to the new location, bring the database online using below command:
ALTER DATABASE r_pub SET ONLINE
As a last step, go back to the SQL Server Agent Job Activity Monitor to start the replication jobs, log reader and distribution agent, as shown in the screen shot below:
At this point you should see that the transactional replication is running without any issues.
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Friday, June 17, 2011 - 10:37:47 AM - Kotesh | Read The Tip |
|
Nice article on replication...crystal clear explaination with screen shots :-) |
|
| Friday, June 17, 2011 - 7:14:04 PM - Papy Normand | Read The Tip |
|
Maybe i am going wrong but according to http://msdn.microsoft.com/en-us/library/ms188031(v=SQL.105).aspx , it seems that only sp_attach_db is depreciated , sp_detach_db is not depreciated. Anyway, the new way with alter database seems to be simpler as after the use of sp_detach procedure, we have to use a create database for attach ( 2 commands instead only one , and the create database for attach is not simple ) Thanks especially for the screen shots ( useful for people for whom english is not the "normal" language... |
|
| Monday, June 20, 2011 - 4:28:14 AM - Mohammed Moinudheen | Read The Tip | ||
|
Papy, Thank you for your comments. Regarding sp_detach_db, below is the excerpt from books online.
This is the link: ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/abcb1407-ff78-4c76-b02e-509c86574462.htm Thanks, |
|||
| Monday, June 20, 2011 - 6:20:14 PM - Papy Normand | Read The Tip |
|
Hello Mohammed, Please, could you have a look at http://msdn.microsoft.com/en-us/library/ms188031(v=SQL.110).aspx Nowhere i found that sp_detach_db is not depreciated even for Denali http://technet.microsoft.com/en-us/library/ms188031(SQL.110).aspx I had had problems with the link you provide ( maybe it is coming from the documentation installed with a MSDN suscription on a computer ) Anyway, i am waiting for a next article which, i am sure , will be as interesting as this one Have a nice day |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |