文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>MSSQL备份与恢复数据存取过程

MSSQL备份与恢复数据存取过程

时间:2011-03-19  来源:fyen

if exists(
select * from sysobjects
where name='BackupDB' and xtype='p'
)
begin
drop proc BackupDB
end
go
create procedure BackupDB
@dataname nvarchar(1024),
@filename nvarchar(1024)
as
 declare  @sql nvarchar(1024)
 set @sql='backup database '+@dataname+' to disk=N'''+@filename+''' with Format'
 exec sp_executesql @sql
go
if exists(
select * from sysobjects
where name='RestoreDB' and xtype='p'
)
begin
drop proc RestoreDB
end
go
create procedure RestoreDB
@dataname nvarchar(1024),
@filename nvarchar(1024)
as
  declare  @masterpath nvarchar(1024)
  declare  @mdfpath nvarchar(1024)
  declare  @logpath nvarchar(1024)
  declare  @logname nvarchar(1024)
  declare  @sql nvarchar(1024)
  select  @masterpath=filename from sysaltfiles where name='master'
  set @mdfpath=rtrim(replace(@masterpath,'master.mdf',@dataname+'.mdf'))
  set @logpath=rtrim(replace(@masterpath,'master.mdf',@dataname+'_log.ldf'))
  set @logname=@dataname+'_log'

  set @sql=N'alter database '+@dataname+' set offline WITH ROLLBACK IMMEDIATE;'
  exec sp_executesql @sql
  Set @Sql=N'restore database '+@dataname+' from disk=N'''+@filename+''' '
  Set @Sql=@Sql+'with '
  Set @Sql=@Sql+'move N'''+@dataname+''' TO N'''+@mdfpath+''','
  Set @Sql=@Sql+'move N'''+@logname+''' To N'''+@logpath+''',replace'     
  exec sp_executesql @Sql
  set @sql=N'alter database '+@dataname+' set online WITH ROLLBACK IMMEDIATE ;'
  exec sp_executesql @sql
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载