SQL LocalDB - Cannot delete a DB when its files are deleted - ssms

How can I delete a SQL LocalDB database that has had its files delete?
Dropping the database yields this message:
Unable to open the physical file "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80.mdf". Operating system error 2: "2(The system cannot find the file specified.)".
File activation failure. The physical file name "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80_log.ldf" may be incorrect.
Running master.sp_databases actually doesn't show them, but the Management Studio does.

The problem is that you are trying to drop a database when the physical file has been deleted or couldn't be found.
To get around this you can detach the database. Detaching will drop the database without attempting to remove the file from the filesystem.
EXEC sp_detach_db 'My_Db'

I'm assuming you're using (localdb) bundled with SQL Server 2012.
If you're using SQL Server 2014, use (localdb)\MSSQLLocalDB in place of (localdb)\v11.0 below
Open a command prompt
Start the localDb instance if it is not already running: “C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe” start “v11.0″
Drop the localDb database by running the following command: “C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd” -S (localdb)\v11.0 -E -d master -Q “DROP DATABASE [myDatabase]”
You can stop the localDb service now: “C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe” stop “v11.0″
Source: http://kazimnami.azurewebsites.net/techblog/2013/02/27/delete-localdb-database-after-physical-files-have-been-deleted/

Related

Restore BacPac database backup in sql server 2014

I have database backup file with extension .bacpac so how can i restore it in sql server 2014 ?
Two options:
In SQL Server Management Studio, right-click on the Databases folder in the Object Explorer tree. Then click on "Import Data-tier application". The wizard will guide you through the process.
From the command line, run this command: "c:\program files (x86)\Microsoft sql server\120\dac\bin\sqlpackage.exe" /a:import /sf:"PATH_TO_YOUR_BACPAC_FILE" /tcs:"Data Source=YOUR_SERVER_NAME;Initial Catalog=NEW_DATABASE_NAME;Integrated Security=true"
Aside: Note that the example connection string in the command line option above assumes you're using Windows Authentication instead of SQL Server authentication to access the server.

Database wont attach to Microsoft SQL Server Management Studio

I'm trying to attach a database to Microsoft SQL Server Management Studio. I have moved the .MDF file into C:\programfiles\Microsoft SQL Server\MSSQL11.MSSQLEXPRESS\MSSQL\Data
It shows up when I go to the attach screen but I get this error message when I hit 'OK' on the attach screen:
Attach database failed for Server 'DESKTOP-5DHLE7T\sqlexpress'.
(Microsoft.SqlServer.Smo)
Additional information:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\AdventureWorks2012_Log.ldf" failed with the operating system error 3 (The system cannot find the path specified.). (Microsoft SQL Server, Error: 5133)
My instructions say to attach the database to C:\programfiles\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data . However despite completing the exact same installation as the guide I don't have a MSSQL11.MSSQLSERVER path, only the C:\programfiles\Microsoft SQL Server\MSSQL11.MSSQLEXPRESS\MSSQL\Data like I stated earlier.
Is this where my problem lies? If so, where should I be putting the .MDF file before I attach it? Any ideas will be much appreciated
First of all, you are attaching the files to your database server, not Management Studio.
Second, your error is about a log file missing. You actually don´t need the LDF just to attach the file, but you need to do some work to make it happen:
USE [master] GO
-- Method 1: I use this method
EXEC sp_attach_single_file_db #dbname='TestDb',
#physname=N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\YourDBFile.mdf'
GO
Or, you can also do this:
CREATE DATABASE TestDb ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\YourDBFile.mdf')
FOR ATTACH_REBUILD_LOG
GO
The error message is saying the LDF file is missing. You need both the MDF and the LDF.
You can click the second file with type is LDF and hit "Remove"

Unable to connect to SQL Server 2008 R2

I am unable to connect to a local SQL Server 2008 R2 trial version from Visual Studio 2010 Express.
When adding a new connection, I browse to the database file in this location:C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AdventureWorks_Data.mdf, I encounter this error:
Could not open new data base 'C:\PROGRAM FILES\MICROSOFT SQL
SERVER\MSSQL10_50.SQLEXPRESS\MSSQL\DATA
ADVENTUREWORKS_DATA.MDF'.CREATE DATABASE is aborted. An attempt to
attach an auto-named database for file C:\Program Files\Microsoft SQL
Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA AdventureWorks_Data.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share. File activation failure.The
physical file name "C:\Program Files\Microsoft SQL
Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AdventureWorks_Data_log.ldf"
may be incorrect.
The log cannot be rebuilt when the primary file is read-only.
Another guess, you don't by any random chance have another user logged into the same machine that would have the DB file locked by connecting to it in the same way you say you're trying to? I'd imagine the previous comment about that database file already being attached to by the sql service is probably far far more likely, but just for wild chance I'm leaving my idea here for you or anyone else who happens by

failed to load resource file sqlcmd.rll

I have a sql script file which is 1.5GB
i want to run it but i cant use it using SQL Management Console, cause it can't load the file. I came up to the solution that run the query with SQLCMD utility but when i want to execute the file it returns (Failed to load resource file SQMCMD.rll)
sqlcmd -s (local) -i C:\myScript.sql
anyone knows what is the problem and how i can run this large script file ?
I had the same problem, and I have solved.
In my case the problem was I had installed more than one version of SQL (2005, 2008, 2012), and I had uninstalled the two first ones, so at this moment I only have the Sql Sever Express 2012.
After playing a lot of minutes I discovered that old installations left his respective sqlcmd.exe files in my machine, but the uninstall process removed the corresponding rll file.
When I hit in the cmd window the sqlcmd it seems the system tries to run the sqlcmd for one of the old sql installation.
Then I went to the folder of the installed version and that works.
It is (in my case, in your case you can find the file sqlcmd.exe):
1- cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn
Inside that folder, then
2- SQLCMD -d databaseName -i C:\MyScripts\specificScript.sql
And that did the trick!
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=228de03f-3b5a-428a-923f-58a033d316e1&displaylang=en
I'd download the sqlcmd installer and run it - see if that fixes you up.
It's under "Microsoft SQL Server 2008 Command Line Utilities".
The installer should create that file and you'll be set. You could also try to run osql, which is the older command line SQL utility.
Edit:
Try this:
Please check the permission on the sqlcmd.exe and sqlcmd.rll and make sure the user has the read&execute permission on them. You also could specify the full path of the sqlcmd.exe in the command, such as:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe" -L
I just had this problem on SQL Server 2008 R2. Reinstalling the Command Line Utilities from the Feature Pack is what did it. The trick is if you are on 32-bit only install the x86 package, but if you are on 64-bit install both the x86 and the x64 package. The reason for this being is that some SQL Server tools are only 32-bit which means that both C:\Program Files\Microsoft SQL Server\100\Tools\Binn and C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn are in your PATH variable in order to ensure all of the tools are accessible, which means either of them could be run, therefore both of them need to reinstalled to ensure you have them run properly.
In my case the issue arised after migrating from SQL Server 2008 R2 to SQL Server 2012.
The problem was pretty simple. It was the path. Just correct your SQL Server path in the System path pointing to the new directory.
I got a variant of this error in an Linux installation of the MS SQL Server CLI for Linux, which stated,
# bcp
Unable to load BCP resource DLL. BCP cannot continue.
# sqlcmd
Unable to load SQLCMD resource file(s)
I found that I had not followed the installation direction exactly, in that rather than modify the system PATH to add the installation directory (/opt/mssql-tools/bin), I had used 'install' to copy the binaries to /usr/local/bin without the corresponding /opt/mssql-tools/share directory.
for I in /opt/mssql-tools/bin/*; do install $I /usr/local/bin; done
When I removed those copies and did export PATH="$PATH:/opt/mssql-tools/bin" as the instructions show, then it worked.
# bcp
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[...]
# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 17.1.0000.1 Linux
[...]

SQL Server 2005 backup restore failing (with folder permissions)

I am trying to restore a database (from file thedb.bak). I am using SQL Server Express edition 2005 on a Windows 7 Ultimate 64-bit machine.
When I try to restore I get the following error:
System.Data.SqlClient.SqlError: The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.2\MSSQL\Thedb.MDF'. (Microsoft.SqlServer.Express.Smo)
My username (antoniocs) is an Administrator. I have edited the permissions in the folder (C:\Program Files (x86)\Microsoft SQL Server\MSSQL.2\MSSQL\) so that the user AntonioCS has full control.
I really need to restore this backup. What am I missing?
Note: I am using the Windows authentication to login. Should I try another user (the one I use is an administrator in the machine)?
The account running the SQL Server service requires permissions on that folder.
You may be connected to SQL Server, but actions are done in the service account context: not you.
Run services.msc from command line, see what account is used, permission this folder accordingly.
Do you have a database that uses Thedb.MDF? I ran into this error when I tried to restore a database over a file that SQL Server used. From the restore window, choose Options and change the path or the filename from the "Restore the database files as".