Database wont attach to Microsoft SQL Server Management Studio - sql

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"

Related

SQL Server 2012 locate backup file not displaying file structure

I have recently installed SQL Server 2012 Express. When I try and restore a previous database, the "Locate Backup File" box is not displaying correctly:
When I maximise the window, it looks like this:
Which means that I can't select a backup. But has anyone else had this issue and how was it resolved?
I'm not sure how to fix the UI problem, but if you want to restore your database, run this code.
RESTORE DATABASE yourDatabaseName
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\Backup\yourDatabaseName.bak';

Why can't I attach an MDF file to SQL Server 2012 Express

I've tried using SSMS but when I click Attach I get error File Not Found. This is AdventureWorks2012_Data.mdf. There is no LDF file. I tried exec'ing this as well:
EXEC sp_attach_single_file_db #dbname='AdventureWorks2012',
#physname = N'R:\SqlServer\AdventureWorks2012_Data.mdf'
GO
but I get the same error:
failed with the operating system error 2 (The system cannot find the file specified.).
TRY the below snippet and you will be through without a .ldf file.
create database AdventureWorks2012_Data
on
(FILENAME='D:\AdventureWorks2012_Data.mdf')
FOR ATTACH_REBUILD_LOG
GO

SQL Server 2012 installation Reporting Services Catalog error

I'm installing SQL Server 2012 at the moment and when I was about to run the installation, this error pops up:
On clicking the first failed test, which is "Reporting Services Catalog Database File Existence", this is what i get:
On clicking the second failed test, which is "Reporting Services Catalog Temporary Database File Existence", this is what i get:
So basically, both of the message box says me that "Catalog Database File" & "Catalog Temporary database files exists". Because of this, i need to select Reporting Services file-only mode installation.
My questions are:
How do i select file-only mode installation?
Do i've to close the setup and do something and afterwards run the setup again?
Btw, I had SQL Server 2012 installed before. I uninstalled it completely due to some strange errors and decided to reinstall a fresh copy of SQL Server 2012 and now I'm stuck with these errors. Any help will be sincerely appreciated. :)
Since you already had one installation of SQL Server done before, there was a database already created. That did not get removed. So when you reinstall, its trying to create the database with the same name, hence, the error. You need to delete the old files to continue the new installation.
From the direcotry
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
Remove the following files
ReportServer.mdf
ReportServer_log.LDF
ReportServerTempDB.mdf
ReportServerTempDB_log.LDF
Try the Following link for further help.
Reporting Services Catalog Error.
For sql-server-2012 the path is:
C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012\MSSQL\DATA
where MSSQL2012 is the instance name and the respective file names are:
ReportServer$MSSQL2012.mdf
ReportServer$MSSQL2012_log.mdf
ReportServer$MSSQL2012TempDB.mdf
ReportServer$MSSQL2012TempDB_log.mdf
Remove the following files from the C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA directory:
bulletReportServer.mdf
bulletReportServer_log.LDF
bulletReportServerTempDB.mdf
bulletReportServerTempDB_log.LDF.
This happens if you reinstall SQL Server and these files weren't deleted.

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

sp_attach_single_file_db Error: failed with the operating system error 5(Access is denied.)

I'm trying to use this DataBase that come with this sample project with from MS:
http://code.msdn.microsoft.com/ASPNET-Web-Forms-6c7197aa/sourcecode?fileId=18930&pathId=365206059
So after I downloaded the files: I need to attach the .mdf DataBase to my Instance of MS SQL 2008.
From Management Studio Attaching DataBase does not work and event using this command i receive the same error:
sp_attach_single_file_db 'School.mdf', 'C:\School.mdf'
ERROR:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\School.mdf" failed with the operating system error 5(Access is denied.).
Any idea what is wrong? Thanks for your help!
Give full permission on folder in which you want to create the mdf file
to the logon account with which the SQL Server service is running.
Usually: NT Service\MSSQL$
You can look that up
Control Panel-> Administrative Tools-> Services=> SQL Server -> Prperties-> Logon Tab-> Note the account
What operating system are you running on? Did you get an elevation prompt when you saved the file to the root of the C drive? What user account is SQL Server running under, and does it have permissions to read any files in the root of the C drive?
You might do better placing the file into %ProgramFiles%\Microsoft SQL Server\MSSQL10.<instance name>\MSSQL\DATA, alongside the other .mdf files that you know it can already read (adjust path above as necessary, but you hopefully get the idea).
I had this issue and all the solutions online was misleading.
The solution was to open SSMS as Administrator. In my opinion first try this and later try all other solutions.
Moving it to DATA folder basically resolved permission issue. Another solution is to change the permissions of the file (or files) and thus still have DB in different folder. I just ran into similar permission error "CREATE FILE encountered operating system error 5(Access is denied.)" trying to attach DB in SQL Server 2012 express on Windows 7.
Solution I did was following:
1) Rich-click on each MDF, LDF, and NDF file to get menu --> select properties
2) Add Users for computer and give them full control (instead of Everyone - to dangerous)
After that I could attach the DB.
However, even if not moved to DATA folder, should move into subfolder instead of C:\ root (example: C:\mydb).
create DATABASE newDataBaseName
on (FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename.mdf'),
(FILENAME = 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\filename_log.ldf')
FOR ATTACH_REBUILD_LOG;
GO
Also as Damien mentioned above put the mdf files in the where your other mdf files are located. For me C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA
In my case I found the Directory lookup needed the computer/users group in read/list permissions. While the service and the actual user running the (in my case) restore had full permissions that permission was important.
Security wise I hate it.