您好,欢迎来到思海网络,我们将竭诚为您提供优质的服务! 诚征网络推广 | 网站备案 | 帮助中心 | 软件下载 | 购买流程 | 付款方式 | 联系我们 [ 会员登录/注册 ]
促销推广
客服中心
业务咨询
有事点击这里…  531199185
有事点击这里…  61352289
点击这里给我发消息  81721488
有事点击这里…  376585780
有事点击这里…  872642803
有事点击这里…  459248018
有事点击这里…  61352288
有事点击这里…  380791050
技术支持
有事点击这里…  714236853
有事点击这里…  719304487
有事点击这里…  1208894568
有事点击这里…  61352289
在线客服
有事点击这里…  531199185
有事点击这里…  61352288
有事点击这里…  983054746
有事点击这里…  893984210
当前位置:首页 >> 技术文章 >> 文章浏览
技术文章

MS SQL server在附加多个.MDF文件时重新建立日志

添加时间:2014-2-13 17:42:36  添加: 思海网络 
现在让我们假设我们已经有了一个数据库叫MyDB1,它有18个.MDF文件和17个.LDF文件。执行下面的命令。

USE [master]
  GO
  /****** Object: Database [MyDB1]  Date: 08/11/2009 11:54:20 ******/
  IF EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDB1')
  DROP DATABASE [MyDB1]
  GO
  CREATE DATABASE MyDB1
  ON
  ( NAME = MyDB1_dat1,FILENAME = 'C:\Data\MyDB1.mdf'),
  ( NAME = MyDB1_dat2,FILENAME = 'C:\Data\MyDB2.mdf'),
  ( NAME = MyDB1_dat3,FILENAME = 'C:\Data\MyDB3.mdf'),
  ( NAME = MyDB1_dat4,FILENAME = 'C:\Data\MyDB4.mdf'),
  ( NAME = MyDB1_dat5,FILENAME = 'C:\Data\MyDB5.mdf'),
  ( NAME = MyDB1_dat6,FILENAME = 'C:\Data\MyDB6.mdf'),
  ( NAME = MyDB1_dat7,FILENAME = 'C:\Data\MyDB7.mdf'),
  ( NAME = MyDB1_dat8,FILENAME = 'C:\Data\MyDB8.mdf'),
  ( NAME = MyDB1_dat9,FILENAME = 'C:\Data\MyDB9.mdf'),
  ( NAME = MyDB1_dat10,FILENAME = 'C:\Data\MyDB10.mdf'),
  ( NAME = MyDB1_dat11,FILENAME = 'C:\Data\MyDB11.mdf'),
  ( NAME = MyDB1_dat12,FILENAME = 'C:\Data\MyDB12.mdf'),
  ( NAME = MyDB1_dat13,FILENAME = 'C:\Data\MyDB13.mdf'),
  ( NAME = MyDB1_dat14,FILENAME = 'C:\Data\MyDB14.mdf'),
  ( NAME = MyDB1_dat15,FILENAME = 'C:\Data\MyDB15.mdf'),
  ( NAME = MyDB1_dat16,FILENAME = 'C:\Data\MyDB16.mdf'),
  ( NAME = MyDB1_dat17,FILENAME = 'C:\Data\MyDB17.mdf'),
  ( NAME = MyDB1_dat18,FILENAME = 'C:\Data\MyDB18.mdf')
  LOG ON
  ( NAME = MyDB1_log1,FILENAME = 'C:\Data\MyDB1.ldf'),
  ( NAME = MyDB1_log2,FILENAME = 'C:\Data\MyDB2.ldf'),
  ( NAME = MyDB1_log3,FILENAME = 'C:\Data\MyDB3.ldf'),
  ( NAME = MyDB1_log4,FILENAME = 'C:\Data\MyDB4.ldf'),
  ( NAME = MyDB1_log5,FILENAME = 'C:\Data\MyDB5.ldf'),
  ( NAME = MyDB1_log6,FILENAME = 'C:\Data\MyDB6.ldf'),
  ( NAME = MyDB1_log7,FILENAME = 'C:\Data\MyDB7.ldf'),
  ( NAME = MyDB1_log8,FILENAME = 'C:\Data\MyDB8.ldf'),
  ( NAME = MyDB1_log9,FILENAME = 'C:\Data\MyDB9.ldf'),
  ( NAME = MyDB1_log10,FILENAME = 'C:\Data\MyDB10.ldf'),
  ( NAME = MyDB1_log11,FILENAME = 'C:\Data\MyDB11.ldf'),
  ( NAME = MyDB1_log12,FILENAME = 'C:\Data\MyDB12.ldf'),
  ( NAME = MyDB1_log13,FILENAME = 'C:\Data\MyDB13.ldf'),
  ( NAME = MyDB1_log14,FILENAME = 'C:\Data\MyDB14.ldf'),
  ( NAME = MyDB1_log15,FILENAME = 'C:\Data\MyDB15.ldf'),
  ( NAME = MyDB1_log16,FILENAME = 'C:\Data\MyDB16.ldf'),
  ( NAME = MyDB1_log17,FILENAME = 'C:\Data\MyDB17.ldf')
  GO

  让我们分离这个数据库并删除所有的.MDF 文件。

 use master
  go
  sp_detach_db MyDB1
  go
  exec master..xp_cmdshell 'Del c:\data\mydb1.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb2.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb3.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb4.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb5.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb6.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb7.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb8.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb9.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb10.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb11.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb12.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb13.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb14.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb15.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb16.ldf'
  go
  exec master..xp_cmdshell 'Del c:\data\mydb17.ldf'
  go

  注意:我使用xp_cmdshell来删除.ldf文件。如果你没有激活xp_cmdshell,那么你会得到下面的错误。

  错误:

  Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1

  SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell'

  because this component is turned off as part of the security configuration for this server.

  A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more

  information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

  你可以使用下面的事务SQL语句来激活xp_cmdshell 。

  use master

  go

  sp_configure 'show advanced options',1

  go

  reconfigure with override

  go

  sp_configure 'xp_cmdshell',1

  go

  reconfigure with override

  go

  或者,你可以在MS-DOS命令提示符中使用Windows Explorer的“Del”命令来删除.ldf文件。

  现在,让我们使用sp_attach_db试着只附加.MDF文件。执行下面的命令。

  sp_attach_db 'MyDB1','C:\Data\MyDB1.mdf',

  'C:\Data\MyDB2.mdf','C:\Data\MyDB3.mdf',

  'C:\Data\MyDB4.mdf','C:\Data\MyDB5.mdf',

  'C:\Data\MyDB6.mdf','C:\Data\MyDB7.mdf',

  'C:\Data\MyDB8.mdf','C:\Data\MyDB9.mdf',

  'C:\Data\MyDB10.mdf','C:\Data\MyDB11.mdf',

  'C:\Data\MyDB12.mdf','C:\Data\MyDB13.mdf',

  'C:\Data\MyDB14.mdf','C:\Data\MyDB15.mdf',

  'C:\Data\MyDB16.mdf','C:\Data\MyDB17.mdf',

  'C:\Data\MyDB18.mdf'

  结果:

  Msg 8144, Level 16, State 2, Procedure sp_attach_db, Line 0

  Procedure or function sp_attach_db has too many arguments specified.

  因为我们将附加更多的文件,所以我们不使用sp_attach_single_file_db系统存储过程。所以让我们使用带有“for ATTACH_REBUILD_LOG”的“Create database”语句试着附加它们。执行下面的事务SQL语句。

 CREATE DATABASE MyDB1
  ON
  (FILENAME = 'C:\Data\MyDB1.mdf'),
  (FILENAME = 'C:\Data\MyDB2.mdf'),
  (FILENAME = 'C:\Data\MyDB3.mdf'),
  (FILENAME = 'C:\Data\MyDB4.mdf'),
  (FILENAME = 'C:\Data\MyDB5.mdf'),
  (FILENAME = 'C:\Data\MyDB6.mdf'),
  (FILENAME = 'C:\Data\MyDB7.mdf'),
  (FILENAME = 'C:\Data\MyDB8.mdf'),
  (FILENAME = 'C:\Data\MyDB9.mdf'),
  (FILENAME = 'C:\Data\MyDB10.mdf'),
  (FILENAME = 'C:\Data\MyDB11.mdf'),
  (FILENAME = 'C:\Data\MyDB12.mdf'),
  (FILENAME = 'C:\Data\MyDB13.mdf'),
  (FILENAME = 'C:\Data\MyDB14.mdf'),
  (FILENAME = 'C:\Data\MyDB15.mdf'),
  (FILENAME = 'C:\Data\MyDB16.mdf'),
  (FILENAME = 'C:\Data\MyDB17.mdf'),
  (FILENAME = 'C:\Data\MyDB18.mdf')
  for ATTACH_REBUILD_LOG
  GO
  结果:
  File activation failure. The physical file name "C:\Data\MyDB1.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB2.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB3.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB4.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB5.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB6.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB7.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB8.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB9.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB10.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB11.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB12.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB13.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB14.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB15.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB16.ldf" may be incorrect.
  File activation failure. The physical file name "C:\Data\MyDB17.ldf" may be incorrect.
  New log file 'C:\Data\MyDB1_log.LDF' was created.

  本篇文章介绍了在附加多个.MDF文件时怎样重建日志。

关键字:SQL server、日志、附加

分享到:

顶部 】 【 关闭
版权所有:佛山思海电脑网络有限公司 ©1998-2024 All Rights Reserved.
联系电话:(0757)22630313、22633833
中华人民共和国增值电信业务经营许可证: 粤B1.B2-20030321 备案号:粤B2-20030321-1
网站公安备案编号:44060602000007 交互式栏目专项备案编号:200303DD003  
察察 工商 网安 举报有奖  警警  手机打开网站