移动数据库物理文件 Move Physical Files

移动数据库物理文件分为移动系统数据库物理文件和移动用户数据库物理文件。[@more@]

在SQL Server 2000中,只能用alter database DB_NAME modify file移动tempdb数据库物理文件,也既只能对 tempdb 数据库中的文件指定 FILENAME。新 tempdb 文件名只有在 SQL Server 停止并重新启动后才能生效。

  1. 确定 tempdb 数据库的逻辑文件名。
USE tempdb
GO
EXEC sp_helpfile
GO
--The logical name for each file is contained in the NAME column.
  1. 使用 ALTER DATABASE 更改每个文件的位置。
USE master
GO
ALTER DATABASE tempdb 
MODIFY FILE (NAME = tempdev, FILENAME = 'E:SQLDatatempdb.mdf')
GO
ALTER DATABASE  tempdb 
MODIFY FILE (NAME = templog, FILENAME = 'E:SQLDatatemplog.ldf')
GO
  1. 停止并重新启动 SQL Server。

要移动用户数据库,请使用 sp_detach_dbsp_attach_db
EXEC sp_detach_db 'pubs', 'true'
-- copy to others drive
EXEC sp_attach_db @dbname = N'pubs',
@filename1 = N'c:Program FilesMicrosoft SQL ServerMSSQLDatapubs.mdf',
@filename2 = N'c:Program FilesMicrosoft SQL ServerMSSQLDatapubs_log.ldf'

在SQL Server 2005中,似乎用户数据库也可以用alter database来移动。脚本:

use databasenName
select name,physical_name from sys.database_files
go

/*** Getting ready to go down to bussiness ****/
--ponint to master db
use Master
go
-- Rollback all trans
--Ensure all users are notify of the down time

alter database DatabaseName set single_user with rollback immediate
go
-- Set databae Offline
alter database DatabaseName set Offline
go
--- Cut and paste the files from the original location to the new location
-- Be sure to write the name and new path exactly
alter database DatabaseName
Modify file (Name = logicalName,Filename = 'DriveLetter:newPathDataFileName.mdf/ndf or ldf')--Create one per every file
go
---Set database backonline
alter database DatabaseName set Online
go
--Set Database back to multi_user
alter database DatabaseName set multi_user
go


/***Script to move file to a new location ***/
--Find Database Physical and logical name
--Write result down very important
sp_helpfile ---Use this OR

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/66009/viewspace-1012027/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/66009/viewspace-1012027/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值