Sql-server – I had to drop and restore database again. I was able to drop but got error while restoring. can any one please help me how do i solve this issue

sql server

use [master]

drop database [databasename]

I got following error

Database could not be dropped because
the database is in use

then i ran following script to deop database and it worked.

use [master]
go
Alter Database [databasename]
SET SINGLE_USER With ROLLBACK IMMEDIATE
go
go
drop database [databasename]
go

then i ran following script and got the error part of which is below the part of script

use[master]
IF
EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'databasename')
DROP

DATABASE [databasename]
GO

i had 5 such drops i took some out here …..for the posting purpose

RESTORE

DATABASE [databasename] FROM
DISK 

= N'C:\dbbak\mybatabase.bak' WITH
FILE

= 1,
MOVE

N'my.C.Data' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\databasename.MDF',
MOVE

N'my.C.Log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\databasename.ldf',
NOUNLOAD

, REPLACE, STATS = 10

i had some more restores in same manner.

the error i am getting now is

Database 'databasename' does not
exist. Make sure that the name is
entered correctly.

Msg 911, Level 16, State 1, Line 1

Database 'databasename' does not
exist. Make sure that the name is
entered correctly.

Msg 911, Level 16, State 1, Line 1

Best Answer

Are you trying to restore or attach?

Are your log files corrupted? Did you try running a DBCC CheckDB beforehand?

What does your DB restore command look like?

BACKUP DATABASE [DBNAme] TO DISK = N'C:\mylocation\DBName.BAK' WITH NOFORMAT, NOINIT, MEDIANAME = N'MediaSet', NAME = N'Database-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO

Check and make sure your logical names are the same.

I would also check your locations. Are all your bak files in C:\dbbak\mybatabase.bak?

Are you sure of restore location - C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA ?