下面的语句可以统计出非系统数据库的文件的文件名,大小,物理路径等信息,方便重装时的文件信息的备份保存:
代码:
select d.name as database_name, f.name as filename, convert(int,round(f.size*8.0/1024,0)) as file_size,rtrim(f.filename) as phyname
from master..sysaltfiles as f, master..sysdatabases as d
where f.dbid = d.dbid
and d.name not in (‘master’,’tempdb’, ‘model’, ‘msdb’, ‘NorthWind’)
下面的语句生成重装数据库服务器后附加数据库的脚本(前提是数据文件的路径没有变化):
代码:
select ‘sp_attach_db ”’ + d.name + ”’,”’ + rtrim(f.filename) + ””
from master..sysaltfiles as f, master..sysdatabases as d
where f.dbid = d.dbid
and d.name not in (‘master’,’tempdb’, ‘model’, ‘msdb’, ‘NorthWind’)
and fileid = 1