22,209
社区成员
发帖
与我相关
我的任务
分享
-- 建测试数据库u010
create database [u010] on primary
(name=N'u010', filename=N'D:\SQLDATA\u010.mdf')
log on
(name=N'u010_log', filename=N'D:\SQLDATA\u010_log.ldf')
-- 卸离数据库
exec dbo.sp_detach_db @dbname=N'u010'
-- 确认日志文件存在: File Exists=1
exec dbo.xp_fileexist 'D:\SQLDATA\u010_log.ldf'
/*
File Exists File is a Directory Parent Directory Exists
----------- ------------------- -----------------------
1 0 1
(1 row(s) affected)
*/
-- 删除日志文件
exec xp_cmdshell 'del D:\SQLDATA\u010_log.ldf'
-- 确认日志文件已删除: File Exists=0
exec dbo.xp_fileexist 'D:\SQLDATA\u010_log.ldf'
/*
File Exists File is a Directory Parent Directory Exists
----------- ------------------- -----------------------
0 0 1
(1 row(s) affected)
*/
-- 缺少日志文件情况下,附加数据库.
create database u010 on
(filename=N'D:\SQLDATA\u010.mdf') --> 只指定数据文件
for attach
-- 结果提示: 新日志文件D:\SQLDATA\u010_log.LDF已创建.
/*
File activation failure. The physical file name "D:\SQLDATA\u010_log.ldf" may be incorrect.
New log file 'D:\SQLDATA\u010_log.LDF' was created.
*/
-- 确认数据库附加成功.
select fileid,name,filename
from u010.dbo.sysfiles
/*
fileid name filename
------ --------------- ----------------------------
1 u010 D:\SQLDATA\u010.mdf --> 原数据库的数据文件
2 u010_log D:\SQLDATA\u010_log.LDF --> 刚自动新建的日志文件
(2 row(s) affected)
*/
USE master;
ALTER DATABASE DBName
SET SINGLE_USER;
运行这个设置成单用户模式,然后把数据分离,分离的时候选 删除连接,再用上面的代码附加试试。---只有mdf文件时附加附加数据库
exec sp_attach_single_file_db DBNAME,'E:\ProduceManageSystem_Data.MDF'
使用这个存储过程看看~