Skip to content

Instantly share code, notes, and snippets.

@MartinMiles
Created January 18, 2021 21:47
Show Gist options
  • Save MartinMiles/a6a69f92425843d2da002c25488b8151 to your computer and use it in GitHub Desktop.
Save MartinMiles/a6a69f92425843d2da002c25488b8151 to your computer and use it in GitHub Desktop.
Re-attach SQL database from an MDF file (good for detaching for removing huge LDF)
-- you should have you DB up& running (for example, after restoring)
-- then you need to detach the database and delete that huge .LDF file
-- it will get re-created upon re-attaching by using the below command
CREATE DATABASE databasename
ON (FILENAME= 'c:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\databasename_web.mdf')
FOR ATTACH_REBUILD_LOG
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment