Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't attach database in SSMS

Tags:

sql-server

For my homework assignment I'm told to download a zip folder holding a .mdf file for the database I'm going to be working on. So I extracted the file and saved it. I then went into SSMS went to attach the database and I got this error?

===================================

Attach database failed for Server 'DESKTOP-VDSUPUV\SQLEXPRESS'.  (Microsoft.SqlServer.Smo)

------------------------------
For help, click: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=15.0.18206.0+((SSMS_Rel).191029-2112)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Attach+database+Server&LinkId=20476

------------------------------
Program Location:

   at Microsoft.SqlServer.Management.Smo.Server.AttachDatabase(String name, StringCollection files)
   at Microsoft.SqlServer.Management.SqlManagerUI.AttachDatabaseData.PrimaryFile.Attach()
   at Microsoft.SqlServer.Management.SqlManagerUI.AttachDatabase.SendDataToServer()

===================================

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------
Program Location:

   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry)
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType, Boolean retry)
   at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries, Boolean retry)
   at Microsoft.SqlServer.Management.Smo.Server.AttachDatabaseWorker(String name, StringCollection files, String owner, AttachOptions attachOptions)
   at Microsoft.SqlServer.Management.Smo.Server.AttachDatabase(String name, StringCollection files)

===================================

Unable to open the physical file "C:\Users\Public\Documents\Databases\PR_log.ldf". Operating system error 2: "2(The system cannot find the file specified.)".
CREATE DATABASE failed. Some file names listed could not be created. Check related errors. (.Net SqlClient Data Provider)

------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=13.00.5026&EvtSrc=MSSQLServer&EvtID=5120&LinkId=20476

------------------------------
Server Name: DESKTOP-VDSUPUV\SQLEXPRESS
Error Number: 5120
Severity: 16
State: 101
Line Number: 1


------------------------------
Program Location:

   at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry)

I downloaded only one .mdf file there is no log file. I was told however when I try to attach,if there isn't a log file then one is created automatically. In this pic that's what it looks like?enter image description here


1 Answers

There are two options to do this:

  1. When you attach the mdf file, in the bottom pane, choose the LDF file and remove it and choose OK. The database will be attached. Reference

Removing LDF file

  1. You can use TSQL to attach database.
USE [master]
GO
CREATE DATABASE [PR] ON 
( FILENAME = N'C:\Users\Public\Documents\Databases\PR.mdf' )
FOR ATTACH_REBUILD_LOG 
GO
like image 61
Venkataraman R Avatar answered Oct 31 '25 07:10

Venkataraman R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!