I have program that uses SQL. For some reason the DB was on a USB stick.
Now we want the DB to be on the hard drive. From the DB manager I detached the database, copied the .mdf file to the HD and tried to reattach the database. However I am getting this error:
Where G:\ is the path of the USB stick.
Where is it getting the old path from? Is it written somewhere in the .mdf file?
You copied the .LDF to the same location that you copied the .MDF, right? You don’t mention that - just the .MDF - so not sure if that’s an oversight or if you didn’t actually copy both files. You need both of them, not just the .MDF.
Is it possible that the LDF isn’t the correct LDF file? I’m curious because you mention the name difference.
As far as I know, SQL just looks for an LDF in the same location as the MDF, with the same name. Not sure if you can change that or not, but it is odd that it’s looking at G:. Hmmmm…
It looks to me like someone gave the log file an odd name, so that’s probably what needs to be fixed. It could also be the default log path set on the server, if you didn’t put the LDF file where all the others are.
The db file does contain the LDF file information, but SQL can’t read that before the database is attached. It uses a default naming convention and path when it’s trying to attach files.
If your LDF filename or path is non-standard, you have to set it manually.
I can post TSQL to do the attach if the GUI fix listed above doesn’t work, just let me know.
If you are unable to get this to work by detach/attach, try backing up the database and then restoring to the new file location. You can put the files wherever you want during the restore.
Yeah, that’s looking like possibly a corrupt database. Or at least a corrupt logfile.
Do you still have the original database up and running? Do you have any backup copies of the database?
If so, the easiest would be to do a backup/recovery rather than detach/attach.
You can attach just the mdf file without using the ldf at all. If the logfile is the only thing corrupted, that might get ya working. Let me go find some code…
After some research, that method (attaching without the logfile) is specifically not recommended when you’re having corruption issues.
Unless the file was corrupted while copying over from the USB stick (which should be solved by re-copying the files per your last post), at this point I’d say your options are down to (1) restore from a good backup, or (2) hire a pro.
If your original database is still up (or you can get it back up), run a ‘DBCC checkdb’ on it and clear up any issues. Then take a new backup and restore to your new location from the validated backup.
If you can’t use the original database but have a backup for it, restore that backup and then run the DBCC checkdb.
Otherwise, you’re going to need some expert assistance to get out of this one. Like these folks.
You could try asking in some of the SQL specific forums. Some of those guys might be able to walk you through it. Or many of them are consultants for hire just for these sorts of problems.
Why did you do detach instead of backup? Typically detach/reattach is only useful if the DB stays in the same path on the same server. (I know you can use it to move DBs, but I don’t think it’s very reliable for that.)
The normal way to do what you want is to backup the database from the USB key, then restore from the backup to the new location.
Seconded. Detach/attach can be quicker if you’re dealing with a very large database, but it doesn’t really have many advantages you’d typically encounter otherwise.
For some reason, detach/attach is the go-to method for a lot of people (including helpdesks) who don’t know much about SQL. And a lot of software vendors use MSSQL but still don’t know crap about it. That was the first way I learned to copy databases, because somebody told me to do it that way.
Aside from the hassle factor, it’s also a lot riskier. Nobody ever (*) screwed up a database by making a backup.
(*)OK, someone, somewhere has probably managed this. I don’t know how, but I’ll bet they have. People can do amazing things when they try hard enough.
This is a little bit of a tangent, but here’s how you mess up with backup: while running the backup, the database is still live and transactions can be executed against it. If you don’t run things right, you can lose some of those transactions when you restore the database. The backup process is usually smart enough to deal with this: it first backs up the database (which doesn’t include the new transactions), then backs up the log as the final step (which does include them). Then when it restores the database, it applies the new transactions to the recovered DB.
But you can still mess this up - maybe the database is in Simple recovery mode, or you don’t take the database offline before the backup is completed. Detach/attach avoids this by taking the database offline immediately.
This is by no means an argument for using detach/attach instead of backup/restore. Instead just run the backup/restore correctly, and be aware of the potential pitfalls.