Background Story:
One of my friends recently called up and asked me if I had spare time to look at his database and give him a performance tuning advice. Because I had some free time to help him out, I said yes. I asked him to send me the details of his database structure and sample data. He said that since his database is in a very early stage and is small as of the moment, so he told me that he would like me to have a complete database. My response to him was “Sure! In that case, take a backup of the database and send it to me. I will restore it into my computer and play with it.”

He did send me his database; however, his method made me write this quick note here. Instead of taking a full backup of the database and sending it to me, he sent me only the .mdf (primary database file). In fact, I asked for a complete backup (I wanted to review file groups, files, as well as few other details).  Upon calling my friend,  I found that he was not available. Now,  he left me with only a .mdf file. As I had some extra time, I decided to checkout his database structure and get back to him regarding the full backup, whenever I can get in touch with him again.

Technical Talk:
If the database is shutdown gracefully and there was no abrupt shutdown (power outrages, pulling plugs to machines, machine crashes or any other reasons), it is possible (there’s no guarantee) to attach .mdf file only to the server. Please note that there can be many more reasons for a database that is not getting attached or restored. In my case, the database had a clean shutdown and there were no complex issues. I was able to recreate a transaction log file and attached the received .mdf file.

There are multiple ways of doing this. I am listing all of them here. Before using any of them, please consult the Domain Expert in your company or industry. Also, never attempt this on live/production server without the presence of a Disaster Recovery expert.

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\TestDb.mdf'
GO
-- Method 2:
CREATE DATABASE TestDb ON
(FILENAME =N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDb.mdf')
FOR ATTACH_REBUILD_LOG
GO

Method 2: If one or more log files are missing, they are recreated again.

There is one more method which I am demonstrating here but I have not used myself before. According to Book Online, it will work only if there is one log file that is missing. If there are more than one log files involved, all of them are required to undergo the same procedure.

-- Method 3:
CREATE DATABASE TestDb ON
( FILENAME =N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDb.mdf')
FOR ATTACH
GO

Please read the Book Online in depth and consult DR experts before working on the production server. In my case, the above syntax just worked fine as the database was clean when it was detached. Feel free to write your opinions and experiences for it will help the IT community to learn more from your suggestions and skills.

PS: Do not try this on production server.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

SQL SERVER – Attach mdf file without ldf file in Database的更多相关文章

  1. 在docker下SQL Server attach mdf和ldf文件

    (DB:MyPost) USE masterGO-- Create database via attachCREATE DATABASE [MyPost]    ON ( FILENAME = N'C ...

  2. SQL SERVER 查看mdf ldf文件路径

    SQL SERVER 查看mdf ldf文件路径 select filename from sysfiles

  3. SQL server 2008 安装报错 reporting services catalog database file existence

    SQL server 2008 安装时报错 提示 reporting services catalog database file existence 查了一下,是因为原来装过Sql server 2 ...

  4. Sql Server 2005 mdf、ldf文件无法复制问题

    [问题原因]Sql Server服务只要启动就一直占用,故无法进行编辑操作. [解决办法 - 1]: 1)在开始-运行对话框中输入"services.msc”,显示如下界面: 2)关闭如上选 ...

  5. SQL SERVER 只有MDF文件的恢复

    方式一: .create a database same name as .mdf file; .Stop SQL Server; . recover .mdf file; . Start SQL S ...

  6. SQL Server导出MDF数据库文件

    更新日志 2022年6月13日 发布. 2022年6月2日 开始. 一句话总结:先分离,然后复制. 先分离要导出mdf数据库文件的数据库. 在Microsoft SQL Server Manageme ...

  7. SQL Server中2008及以上 ldf 文件过大的解决方法

    在SQL Server中经常遇到事务日志变大的情况,除了将数据库设置为“自动收缩”外,还可以使用下面的SQL命令进行快速清除数据库中的事务日志,命令如下:  - 第一步:清空日志  ALTER DAT ...

  8. SQL Server 导入 MDF LDF文件

    EXEC sp_attach_db @dbname = 'OA', @filename1 = 'C:\OA.mdf', @filename2 = 'C:\OA_log.ldf'

  9. sql server导入mdf 报操作系统错误 5:“5(拒绝访问。)”

    错误一:拒绝访问 在安装示例库时出现以下的错误 消息 5120,级别 16,状态 101,第 1 行无法打开物理文件"D:\Download\AdventureWorks2012_Data. ...

随机推荐

  1. Sql Server 系统表功能

    SELECT Name FROM Master..SysDatabases ORDER BY Name

  2. When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath

    The most important difference is that the forIndexPath: version asserts (crashes) if you didn't regi ...

  3. spring boot 拦截器

    @SpringBootApplicationpublic class Application extends WebMvcConfigurerAdapter { public static void ...

  4. debian系统root用户登录

    Debian默认不允许root登录,所以修改之. 让Debian以root登录 1).首先修改gdm3的设定文件(/etc/gdm3/deamon.conf),在[security]字段后面追加如下一 ...

  5. Ink – 帮助你快速创建响应式邮件(Email)的框架

    Ink 可以帮助你快速创建响应的 HTML 电子邮件,可工作在任何设备和客户端.这个 CSS 框架帮助您构建可在任何设备上阅读的 HTML 电子邮件.曾经需要你兼顾各种邮件客户端的日子一去不复返了,I ...

  6. 使用Eclipse调试PHP程序

    我安装的是PHP Version 5.3.26,按照网上提示在Eclipse中使用XDebug进行调试,不过配置了却使用不了,下面把解决方法简要说一下. XDebug老是加载不了 From PHP 5 ...

  7. JavaScript对象字面量

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. Apache log4net™ 手册——概述【翻译】

    原文地址 本文内容 概述 框架 日志(Loggers)和追加器(Appenders) 日志层次(Logger hierarchy) 追加器(Appenders) 筛选(Filters) 布局(Layo ...

  9. sql date()函数

    SQL Server Date 函数 下面的表格列出了 SQL Server 中最重要的内建日期函数: 函数 描述 GETDATE() 返回当前的日期和时间 DATEPART() 返回日期/时间的单独 ...

  10. 使用.NET统计文件夹中文件总数

    软件下载: http://hovertree.com/h/bjaf/hwqtjwjs.htm 截图: 使用方法:点击按钮,选择文件夹,就可以显示文件夹中包含的文件总数. 这个项目包含在HoverTre ...