Question

SQL 2005 Express I want to backup database by detaching then reattaching database when the backup has run.

Asked by: RDAdams

I have a new server install of SQL 2005 Express.  It has a currently empty database.  I am looking at the best way to backup the files using our Tivoli server (without spending extra money).  Since the database is only used by internal clients I want to simply detach the database say at 9pm each day.  Allow the backup to run.  Then reattach the database at 6am each day.  

I do not care that the database will be unavailable for clients during the detached period.  the following three lines show the error I get just trying to detach.  Can someone assist?  

E:\Apps\Seisware>sqlcmd -d DB_Name -E -Q "sp_detach_db 'DB_Name'"
Msg 3703, Level 16, State 3, Server HCICALAS07, Line 1
Cannot detach the database 'DB_Name' because it is currently in use.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-06-04 at 07:45:36ID24463771
Topics

SQL Server 2005

,

MS SQL Server

Participating Experts
5
Points
500
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. what is the syntax for sp_detach
    what is the code to detach a database and reattach a database?
  2. detach all drawings that are xref into a document- manipul…
    I have the following code to open an AutoCAD drawing in Access.... I like it and pretty happy with it working. But I have two questions... When I open a particular drawing, I need it to detach all the drawings that were xref into it... and then at the end of my manipulation...
  3. MDI child window detaching/reattaching (broken Client Ar…
    I have come across a rather obscure set of issues with Windows Forms in C#, which I believe are related (perhaps the same issue). It occurs when I have a main form and a set of MDI child forms that it manages. The problem: After some time, when clicking on the Client Area...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ged325Posted on 2009-06-04 at 07:48:41ID: 24547565

don't use -d DB_name, use master.

Essentially you are connecting to the database, then trying to detach it.  But since you're connected you block yourself.  

The other (better) way of doing this is to run a backup job with sqlserveragent.  The output of this is a BAK file which you can then do a file backup.  (EG: run the db backup at 8, then the filebackups come along at 9).

 

by: pcelbaPosted on 2009-06-04 at 07:53:35ID: 24547622

Instead of detaching you could stop the SQL Server service and backup .mdf and .ldf files.

To detach the database you need exclusive access to it:

To detach a database
In SQL Server Management Studio Object Explorer, connect to the instance of the SQL Server Database Engine and then expand the instance.

Expand Databases, and select the name of the user database you want to detach.

Detaching a database requires exclusive access to the database. If the database is in use, restrict access to a single user:

Right-click the database name and point to Properties.

In the Select a page pane, select Options.

In the Other options pane, scroll down to the State options.

Select the Restrict Access option, and in its drop-down list, select Single.

Click OK.

A message box appears to inform you that this action will close all connections to the database. To proceed, click OK.

Right-click the database name, point to Tasks, and then click Detach. The Detach Database dialog box appears.

The Databases to detach grid displays the name of the selected database in the Database Name column. Verify that this is the database you want to detach.

By default, the detach operation retains any out-of-date optimization statistics when detaching the database; to update the existing optimization statistics, click the Update Statistics check box.

By default, the detach operation keeps any full-text catalogs that are associated with the database. To remove them, clear the Keep Full-Text Catalogs check box.

The Status column displays the current database state (either Ready or Not Ready).

If the status is Not Ready, the Message column displays hyperlinked information about the database. When a database is involved with replication, the Message column displays Database replicated. When a database has one or more active connections, the Message column displays <number_of_active_connections> Active connections; for example, 1 Active connection(s). Before you can detach the database, you must disconnect any active connections by selecting the Drop Connections check box.

To obtain more information about a message, click the hyperlink.

When you are ready to detach the database, click OK.

Note:  
The newly detached database will remain visible in the Databases node of Object Explorer until the view is refreshed. You can refresh the view at any time: Click in the Object Explorer pane, and from the menu bar select View and then Refresh.

Source: MS SQL Help

 

by: aneeshattingalPosted on 2009-06-04 at 07:56:57ID: 24547661

to check for the active connections,  run sp_Help2  in SSMS and you can kill the connections using  KILL <SPID> , spid's against that database

 

by: St3veMaxPosted on 2009-06-04 at 08:16:01ID: 24547867

If the database is off-line; it would be easier to create pre/post scripts that Tivoli calls (if it can) to stop the SQL Server Service, Backup the flat files and restart it once done.

Saves all of the other faffing around.

HTH

 

by: RDAdamsPosted on 2009-06-04 at 08:24:56ID: 24547995

ok, using master works great for detaching the database.

how about reattaching?  The command sqlcmd -d Master -E -Q 'sp_attach_db 'DB_Name'" gives an error
Sqlcmd: '''DB_Name'"': Unexpected argument. Enter '-?' for help.

 

by: RDAdamsPosted on 2009-06-04 at 08:29:17ID: 24548057

St3veMax the database will not be in use at the time the backup needs to run.  But I don't want to pay any extra for the Tivoli software needed to do this.  We are currently in a spending freeze.  I need to work with what I have.  

pcelba - I don't want to duplicate the database using twice as much space by running the internal backup software.  I just want to backup the files that are already created.

All I want is as asked.  Detach the database so my backup will pick up the files....thanks ged325 as what you said works fine for that.  I just need to find the right code to reattach it so I can create a batch file to run as a task on the server.

 

by: St3veMaxPosted on 2009-06-04 at 08:40:55ID: 24548184

RD,

I totally understand as we're in a similar position; hence why I say shutdown the SQL Server thus meaning there's nothing accessing those flat files.

It's a lot easier than messing around with pre/post scripts to detach/attach the database.

NET STOP MSSQLSERVER$EXPRESS --Edit for your instance name

BACKUP FILE

NET START MSSQLServer$Express

Job done...

 

by: pcelbaPosted on 2009-06-04 at 08:43:42ID: 24548216

It is just misunderstanding or insufficient info... I've been recommending to STOP SQL Service and backup files (by Tivoli) because to do the backup in SQL when SQL Service is stopped is impossible. Same thing offered St3veMax.

Dettaching/Attaching the database is not necessary when the service is stopped.

 

by: acperkinsPosted on 2009-06-04 at 10:38:35ID: 24549383

>> I am looking at the best way to backup the files using our Tivoli server (without spending extra money).<<
If you are not going to do a SQL Backup, but instead rely on file backups, make sure you change the default Recovery Model from Full to Simple or you will find that your Transaction Log will very quickly run you out of disk space.

 

by: RDAdamsPosted on 2009-06-04 at 13:21:30ID: 24551045

I will try stopping and starting the services tonight to see how it works.

Thanks will post tomorrow.

 

by: RDAdamsPosted on 2009-06-08 at 11:17:08ID: 24574820

Still looking at this but it seems to work as suggested.

 

by: St3veMaxPosted on 2009-06-08 at 12:59:30ID: 24575717

Job done then isnt it?

 

by: acperkinsPosted on 2009-06-08 at 18:02:01ID: 24577539

And please do not disregard my comment about changing the Recovery Model to Simple or will will see you back here with a question titled something like "Why have I run out of disk space?"

 

by: RDAdamsPosted on 2009-06-09 at 15:14:51ID: 31588809

St3veMax's post was the most complete easy to follow instructions.  Thanks again for assisting all of you.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...