备份:
/* *功能:数据库备份 *dbname:数据库名称 *bakname:备份名称,包含完整路径 */ use master BACKUP DATABASE dbname TO disk='bakName'
还原:
/* *功能:数据库还原 *@dbname:数据库名称 *@bakname:备份名称,包含完整路径 */ use master declare @dbname nvarchar(500) declare @bakname nvarchar(500) set @dbname ='dbname' set @bakname='c:\bakmame' declare @spid nvarchar(20) declare tb cursor for select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) open tb fetch next from tb into @spid while @@fetch_status=0 begin exec('kill '+@spid) fetch next from tb into @spid end close tb deallocate tb RESTORE DATABASE @dbname FROM disk = @bakname with replace
注意:
还原的时候,必须使用use master,使用自身会出错,还原是先“杀掉”连接到要还原的数据库的用户进程,如果使用自身,就会出现“自杀”的情景,会出错。