辅导:移动数据库物理文件MovePhysicalFiles
来源:优易学  2011-10-22 12:28:05   【优易学:中国教育考试门户网】   资料下载   IT书店
  移动数据库物理文件分为移动系统数据库物理文件和移动用户数据库物理文件。
  在SQL Server 2000中,只能用alter database DB_NAME modify file移动tempdb数据库物理文件,青年人网站提示也既只能对 tempdb 数据库中的文件指定 FILENAME。新 tempdb 文件名只有在 SQL Server 停止并重新启动后才能生效。
  确定 tempdb 数据库的逻辑文件名。
  USE tempdb
  GO
  EXEC sp_helpfile
  GO
  --The logical name for each file is contained in the NAME column.
  使用 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
  停止并重新启动 SQL Server。
  要移动用户数据库,请使用 sp_detach_db 和 sp_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

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训