1

我在 SQL Server 2005 中有一个名为 MyStaging 的数据库,我创建了一个名为 MyStaging_bk 的空白数据库。我去了 Backup MyStaging,然后尝试恢复到 MyStaging_bk。但它说我无法恢复,因为备份集不同。

如何复制数据库并且它们在同一数据库服务器上?

4

1 回答 1

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

newdatabase 没有添加任何用户。所以:

use newdatabasename    

create user someuser from login anexistentloginname

sp_addrolemember 'db_owner', 'someuser'
sp_addrolemember 'db_datareader', 'someuser' 
sp_addrolemember 'db_datawriter', 'someuser'

更新1:

您必须单独运行每个块或go在每个块中添加关键字。

更新2:

我在这里不好,新数据库将从备份数据库中获取所有用户。

于 2012-09-05T19:11:08.793 回答