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. 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

    引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...

  2. ODB学习笔记之基础环境搭建

      一,简介 ODB是应用于C++的一个开源.跨平台.跨数据库的对象关系映射(ORM)系统. 它可以让你持久化C++对象到关系数据库,而不必处理表.列或者SQL,无需手动编写任何映射代码. ODB支持 ...

  3. MyBatis知多少(3)

    解决存储过程固有限制的方法之一就是将SQL嵌入到更加通用的语言中去.与存储过程将业务逻辑移入数据库相反,内联SQL将SQL从数据库移入了应用程序代码.这就使得SQL语句可以直接与语言进行交互.从某种意 ...

  4. VirtualBox的网络设置(6种方式)

    VirtualBox 可以为每一个虚拟机分配8个网卡.每一个网卡的连接方式可以选为下列之一: Not attached Network Address Translation (NAT) Bridge ...

  5. LeetCode——Jump Game

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. javascript之IE版本检测

    近年来随着操作系统的升级以及各种新技术的开发普及,抛弃低版本IE已经是大势所趋,这对于前端人员来时是个好消息,可以不用花费太多的时间来做低版本的兼容,很多站点采用给予低版本IE以提示的方式(恩,很友好 ...

  7. 6/20 sprint3 看板和燃尽图的更新

  8. jQuery的 delegate问题

    习惯了bind,用惯了live,就不习惯delegate了呀有木有... 支持为动态生成的标签元素绑定事件也许就live和delegate了吧,不过新版本已经不支持live了,只有delegate d ...

  9. Ajax学习整理

    什么是ajax?W3School中给ajax的定义是: 1.AJAX = 异步 JavaScript 和 XML. 2.AJAX 是一种用于创建快速动态网页的技术. 3.通过在后台与服务器进行少量数据 ...

  10. 双系统Ubuntu无法访问windows磁盘分区解决方法

    为了更好的体验各种操作系统,在电脑中安装双系统是很好的选择,但在使用中难免会遇到这样或那样的问题. 最近总是遇到Ubuntu系统下无法访问windows磁盘分区问题,看了系统日志发现是挂载磁盘出问题了 ...