Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

attaching and detaching db

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to move the transaction log from one drive to another because it is full. I have tried the following script:

sp_detach_ db 'database'
go
sp_attach_db 'database', 'c:\msql7\data\datafile.mdf',
'f:\mssql7\data\logfile.ldf'

but, I get an error message stating that the script can't execute because the database is currently in use. Does anyone know a better way of doing this? Thanx!
 
First of all, how are you doing your backups? Do you need the transaction log or can you truncate it. You can't move a log. Databases are required to have a transaction log, whether you need one or not.

You could truncate the log, you could set the 'truncate log on checkpoint option', you could shrink the log, you could back up the log thereby shrinking it, etc. But, you cannot move the log.

If your recovery needs don't require that you use a log, in that your only need to restore from a database backup, say, made every night at off hours, then maybe you don't need to have a log. But, if you need to have the ability to restore your database up to a certain point in time, then you will need to regularly backup the log for potential recovery purposes.

I would strongly suggest you read books-online and learn about database backup and recovery issues.

Thanks,

Tom
 
From books online:

This example detaches the pubs database with skipchecks set to true.

EXEC sp_detach_db 'pubs', 'true'

This example attaches two files from pubs to the current server.

EXEC sp_attach_db @dbname = N'pubs',
@filename1 = N'c:\mssql7\data\pubs.mdf',
@filename2 = N'c:\mssql7\data\pubs_log.ldf'


So, just change 'pubs' to whatever you call your database and set the location of where your database files are.

This works always. But when it says "the database is in use" you might be accessing the database somehow. Maybe you are browsing in it with the Enterprise Manager or something else.
 
What I have done before, is created an empty shell of a database an attach the files. I followed books on line and it worked.

Cal


 
Sorry, I keep misunderstanding people's questions.. lack of sleep I guess..

Anyways, you cannot detach a database if someone else is connected to it. You could always put the database in single user mode before attempting this as well..

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top