导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

请教restore用法的简单问题,谢谢各位!

ranliang 2002-12-29 09:10:17
在test库中写了如下的存储过程

CREATE PROCEDURE my_proc1 @sql nvarchar(1000) as
restore database test from disk=@sql with replace
GO

然后我在查询分析器test库下写了如下代码,调用以上存储过程用于测试数据的倒入情况,
代码如下(D:\自动备份目录\kkk.bat是test库以前的一个备分,没有问题)

execute my_proc1 'D:\自动备份目录\kkk.bat'

提示错误信息是

服务器: 消息 3101,级别 16,状态 1,过程 my_proc1,行 2
因为数据库正在使用,所以未能获得对数据库的排它访问权。
服务器: 消息 3013,级别 16,状态 1,过程 my_proc1,行 2
RESTORE DATABASE 操作异常终止。

难道我想替换掉当前库里的信息都不可以吗,请教以下,如何用restore替换当前库的信息???




...全文
71 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ranliang 2002-12-29
to:pengdali(大力)
先谢谢
你后两个存储过程,我应该使用过,它门是把表从sysobject先读出来,才一个一个备份,我现在想只是把数据库全部备份成一个.bak的文件,然后用这个.bak 的文件可以做数据倒入,
我先试试,你给我的第一个存储过程,看可否满足我的需求.
再次谢过!!!
回复
pbsql 2002-12-29
还原整个数据库:

RESTORE DATABASE { database_name | @database_name_var }
[ FROM < backup_device > [ ,...n ] ]
[ WITH
[ RESTRICTED_USER ]
[ [ , ] FILE = { file_number | @file_number } ]
[ [ , ] PASSWORD = { password | @password_variable } ]
[ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
[ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
[ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ]
[ ,...n ]
[ [ , ] KEEP_REPLICATION ]
[ [ , ] { NORECOVERY | RECOVERY | STANDBY = undo_file_name } ]
[ [ , ] { NOREWIND | REWIND } ]
[ [ , ] { NOUNLOAD | UNLOAD } ]
[ [ , ] REPLACE ]
[ [ , ] RESTART ]
[ [ , ] STATS [ = percentage ] ]
]

执行时当然不能连接自己了,连master
回复
pengdali 2002-12-29
呵呵!!
回复
dotnba 2002-12-29
跟着大力冲!
回复
pengdali 2002-12-29
create PROCEDURE GY_DBBak
@bakequip int, -- 备份设备:磁盘&磁带
@bakpath varchar(50), -- 带全路径的备份文件名
@baktype int, -- 完全备份&增量备份
@baklog int, -- ‘0’备份日志
@bakdb int, -- ‘0’备份数据库
@kind varchar(7), --备份还是恢复

@retmsg varchar(20) output --返回信息
AS


DECLARE @DevName_data varchar(50)
DECLARE @DevName_log varchar(50)
declare @db_path varchar(100)
declare @log_path varchar(100)

DECLARE @RC INT

SELECT @db_path = @bakpath + '.dat'
SELECT @log_path = @bakpath + 'log.dat'
SELECT @RC=0

DBCC CHECKDB(Northwind)
/***********************************************************
** CREATE BACKUP AND RESTORE DEVICES
************************************************************/
IF @RC=0
BEGIN

EXEC sp_addumpdevice 'disk', @DevName_data,@db_path

exec sp_addumpdevice 'disk', @DevName_log,@log_path
select @rc=@@error
IF @RC<>0
begin
EXEC SP_DropDevice @Devname_data
exec sp_dropdevice @devname_log
SELECT @RC=-1000
return @rc
end
END

IF @kind='backup'
BEGIN
IF @bakequip=0
BEGIN
IF @baktype=0
BEGIN
IF @bakdb=0
BEGIN
BACKUP DATABASE Northwind TO DISK=@Devname_data
WITH INIT
END
IF @baklog=0
BEGIN
BACKUP LOG Northwind WITH NO_LOG
BACKUP LOG Northwind TO DISK=@DevName_log
WITH INIT,NO_TRUNCATE
END
END
ELSE BEGIN
IF @bakdb=0
BEGIN
BACKUP DATABASE Northwind TO DISK=@DevName_data
WITH NOINIT
END
IF @baklog=0
BEGIN
BACKUP LOG Northwind WITH NO_LOG
BACKUP LOG Northwind TO DISK=@DevName_log
WITH NOINIT,NO_TRUNCATE
END
END
END
SELECT @retmsg='数据库备份成功!'
END

IF @kind='restore'
BEGIN
RESTORE DATABASE Northwind FROM DISK= @DevName_data WITH REPLACE
SELECT @retmsg='恢复数据库成功!'
END

RETURN 0







-----------------------------------
CREATE PROCEDURE dt_databasebackup @str_backuppath varchar(255),@str_databasename varchar(50),@str_tablename varchar(50) AS
declare @str_sqlexecute varchar(255)
if @str_tablename=‘‘or @str_tablename=null
begin
declare backuptable_cursor cursor for
select sysobjects.name from sysobjects where sysobjects.type=‘U‘
open backuptable_cursor
fetch next from backuptable_cursor into @str_tablename
while @@fetch_status=0
begin
select @str_sqlexecute=‘bcp ‘+@str_databasename+‘..‘+@str_tablename+‘ out ‘+ @str_backuppath+‘\‘+@str_tablename+‘.bac -c -U sa -P‘
exec master..xp_cmdshell @str_sqlexecute
fetch next from backuptable_cursor into @str_tablename
end
deallocate backuptable_cursor
end
else
begin
select @str_sqlexecute=‘bcp ‘+@str_databasename+‘..‘+@str_tablename+‘ out ‘+ @str_backuppath+‘\‘+@str_tablename+‘.bac -c -U sa -P‘
exec master..xp_cmdshell @str_sqlexecute
end
--数据库数据备份存储过程
go


CREATE PROCEDURE dt_databaserestore @str_restorepath varchar(255),@str_databasename varchar(50),@str_tablename varchar(50) AS
declare @str_sqlexecute varchar(255)
if @str_tablename=‘‘ or @str_tablename=null
begin
declare restoretable_cursor cursor for
select sysobjects.name from sysobjects where sysobjects.type=‘U‘
open restoretable_cursor
fetch next from restoretable_cursor into @str_tablename
while @@fetch_status=0
begin
select @str_sqlexecute=‘truncate table ‘+@str_tablename
execute(@str_sqlexecute)
select @str_sqlexecute=‘bcp ‘+@str_databasename+‘..‘+@str_tablename+‘ in ‘+ @str_restorepath+‘\‘+@str_tablename+‘.bac -c -U sa -P‘
exec master..xp_cmdshell @str_sqlexecute
fetch next from restoretable_cursor into @str_tablename
end
deallocate restoretable_cursor
end
else
begin
select @str_sqlexecute=‘truncate table ‘+@str_tablename
execute(@str_sqlexecute)
select @str_sqlexecute=‘bcp ‘+@str_databasename+‘..‘+@str_tablename+‘ in ‘+ @str_restorepath+‘\‘+@str_tablename+‘.bac -c -U sa -P‘
end
--数据库数据恢复存储过程
go
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告