我在SQL Server 2005中有一个名为MyStaging的数据库,我做了一个名为MyStaging_bk的空白数据库。我去了Backup MyStaging,然后尝试恢复到MyStaging_bk。但它说我无法恢复,因为备份集不同。
如何复制数据库并且它们位于同一个数据库服务器上?
答案 0 :(得分:3)
我正在考虑您可以使用高级别用户登录sql server。
-- go for master
use master
-- backup the old database
backup database olddatabasename
to disk = 'c:\backup.bak'
-- create a new database
create database newdatabasename
on
(
name = newdatabasenamedata,
filename = 'c:\newdatabasenamedata.mdf',
size = 500mb,
maxsize = unlimited,
filegrowth = 25mb
)
log on
(
name = newdatabasenamelog,
filename = 'c:\newdatabasenamelog.ldf',
size = 5mb,
filegrowth = 5mb
)
-- put it in single user mode
alter database newdatabasename
set single_user
with rollback immediate
-- get the backup logical names and write it down
restore filelistonly
from disk = 'c:\backup.bak'
-- restore the database from the backup using the logical names
restore database newdatabasename
from disk = 'c:\backup.bak'
with
move 'olddatabasenamelogicalnamedata' to 'c:\newdatabasenamedata.mdf',
move 'olddatabasenamelogicalnamelog' to 'c:\newdatabasenamelog.ldf',
replace
-- rename the logical names if you want (do it =p)
alter database newdatabasename
modify file
(
name = 'olddatabasenamelogicalnamedata',
newname = 'newdatabasenamedata'
)
alter database newdatabasename
modify file
(
name = 'olddatabasenamelogicalnamelog',
newname = 'newdatabasenamelog'
)
我非常确定从备份中恢复数据库会自动将其置于多用户中,但是:
alter database newdatabasename
set multi_user
新数据库没有添加任何用户。所以:
use newdatabasename
create user someuser from login anexistentloginname
sp_addrolemember 'db_owner', 'someuser'
sp_addrolemember 'db_datareader', 'someuser'
sp_addrolemember 'db_datawriter', 'someuser'
<强> UPDATE1:强>
您必须单独运行每个块,或在每个块中添加go
关键字。
<强> UPDATE2:强>
我的不好,新数据库将从备份数据库中获取所有用户。