solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page

SQL Product Highlight

Idera - SQL diagnostic manager

SQL diagnostic manager is a powerful performance monitoring and diagnostics solution that proactively alerts administrators to health, performance or availability problems within their SQL Server environment via a central console or mobile device. SQL diagnostic manager minimizes costly server downtime by providing agent-less, real-time monitoring and customizable alerting for fast diagnosis and remediation of SQL Server performance and availability problems. SQL diagnostic manager also provides a 'community' environment where, using Idera's IntelliFeed(TM) technology DBAs form a community to collaborate to quickly resolve problems, perform maintenance and capture best practices.

Learn more!








Moving database files for a replicated SQL Server database

By: | 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

Problem

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.

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.


Option 1 - sp_detach_db

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.

running out of disk space on a sql server replicated database and moving them to another database

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.


Option 2 - ALTER DATABASE MODIFY FILE

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:

use the alter database modify file command in ssms

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:

open ssma and navigate to instance sql server agent

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:

go back to sql server agent job activity monitor

At this point you should see that the transactional replication is running without any issues.

Next Steps

  • Avoid using the sp_attach_db and sp_detach_db system stored procedures for attaching or detaching databases because the commands have been deprecated.
  • Get familiar with new options available in SQL Server like alter database with modify file, create database for attach, etc.
  • Check out all of the SQL Server Replication tips.


Related Tips: 1 | 2 | 3 | 4 | 5 | More | Become a paid author


Last Update: 6/17/2011

Share: Share 






Comments and Feedback:

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.

Important:

This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible.

 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



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"SQL doctor is the best SQL product on the market, by far. All of Idera's tools are great, but this is the icing on the cake!"

Quickly and accurately deploy database changes with Red Gate's SQL Compare - the industry standard comparison and deployment tool.

What grade do you think your SQL Servers get? Find out with a SQL Server Health Check consultant.

Free Trial: Get Proactive Insight with Spotlight® for SQL Server Enterprise.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Are you waiting on SQL Server? Learn about these DMV's.


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
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