开发者

SQL Server Script Error: Database is Already in Use

开发者 https://www.devze.com 2023-04-08 14:12 出处:网络
I have a SQL Server deployment script running from visual studio that gives me the error: Error SQL01268: .Net SqlClient Data Provider: Msg 1828, Level 16, State 5, Line 1 The logical file name \"my

I have a SQL Server deployment script running from visual studio that gives me the error:

Error SQL01268: .Net SqlClient Data Provider: Msg 1828, Level 16, State 5, Line 1 The logical file name "myDB"开发者_StackOverflow社区 is already in use. Choose a different name.

However, when I go into SSMS and try and drop this database, I get the error:

Cannot drop the database 'myDB', because it does not exist or you do not have permission.

Can anyone help me understand where this phantom filename is already stored so that I can delete it?

Thanks.


The second error message states that the database cannot be dropped because other sessions are currently connected to it. In order to kick all the users out, you can add this to your deployment script or run it manually before deploying. The USE command is to make sure there isn't a race condition when you are the one who's connected.

USE [master];
GO
ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE myDB;
GO


This error can also occur when trying to generate a database from a script. I was trying to do downgrade a SQL Server 2012 database to 2008 as per this article https://superuser.com/questions/468578/move-database-from-sql-server-2012-to-2008. However, when I ran the script initially, I got this error

The logical file name "emmagarland" is already in use. Choose a different name.

Turns out (after reading this article https://ask.sqlservercentral.com/questions/106577/the-logical-file-name-database-is-already-in-use.html) in my restore script, I hadn't checked and changed the logical file names. I had to ensure the path is correct, and the change the generated name for the log file, else it tries to overwrite the .mdf file with the log and then throws this error. I fixed it by changing the name from xxxxx to xxxxxldf

 CREATE DATABASE [xxxxx] ON  PRIMARY 
 ( NAME = xxxxx', FILENAME = N'C:\Program Files\Microsoft SQL
 Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\xxxxx.mdf' , SIZE =
 14400KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
 ( NAME = N'xxxxxldf', FILENAME = N'C:\Program Files\Microsoft SQL
 Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\xxxxx_log.ldf' ,
 SIZE = 18560KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 GO

Sounds simple but I couldn't work out why this error was occuring when I was creating a brand new database!

0

精彩评论

暂无评论...
验证码 换一张
取 消