How to add a SQL database without the LDF file

SQL-Server-2012 Today I want to add a SQL database to the SQL server in my lab environment. The database I want to add is a sample database that I want to import in my environment to test some query’s.

The download for this sample database only contains a .mdf file and no LDF file (transaction log). When I try to add this database trough the SQL Management Studio I receive an error that the LDF is missing and that because of that the database can’t be added.

I did some Google research to find a way to add this database to my lab environment.
To make this possible I wrote a small TSQL script to add the database to the SQL server in my lab.

USE [master]
GO
EXEC sp_attach_single_file_db @dbname='AdventureWorks2012',
@physname=N'C:\SQL\DATA\AdventureWorks2012_Data.mdf'
GO

The transaction log is recreated and the database is added to the SQL Management Studio. Important side note is to remember that the SQL Service account has to have write rights of the database location folders for this to work.