在SQL Server 中可以使用拆分(Detach)和附加(Attach)的方法來移動(dòng)數(shù)據(jù)庫。拆分?jǐn)?shù)據(jù)庫是從服務(wù)器中移去邏輯數(shù)據(jù)庫,但不會(huì)將操作系統(tǒng)中的數(shù)據(jù)庫文件刪除。附加數(shù)據(jù)庫將會(huì)創(chuàng)建一個(gè)新的數(shù)據(jù)庫,并復(fù)制存儲(chǔ)在已有的數(shù)據(jù)庫文件和事務(wù)日志文件中的數(shù)據(jù)。使用系統(tǒng)存儲(chǔ)過程Sp_detach_db 來拆分?jǐn)?shù)據(jù)庫,用系統(tǒng)存儲(chǔ)過程Sp_attach_db 來附加數(shù)據(jù)庫。
Sp_detach_db 系統(tǒng)存儲(chǔ)過程的語法如下: sp_detach_db [@dbname =] 'database_name' [, [@skipchecks =] 'skipchecks'] 其中[@skipchecks =] 'skipchecks'子句中Skipchecks 的值為True 或False。 當(dāng)Skipchecks的值為True 時(shí),指定在執(zhí)行此過程之前不需要對(duì)數(shù)據(jù)庫中的所有表執(zhí)行UPDATE STATISTICS命令;為False 時(shí),則需要執(zhí)行UPDATE STATISTICS 命令。
Sp_attach_db 系統(tǒng)存儲(chǔ)過程的語法如下: sp_attach_db [@dbname =] 'dbname', [@filename1 =] 'filename_n' [,...16] 其中“filename_n”包括文件的路徑和物理名稱。最多可指定16 個(gè)文件。文件中必須包含主數(shù)據(jù)庫文件。如果需要附加的文件超過了16 個(gè),就必須使用帶FOR ATTACH 子句的CREATE DATABASE 命令來代替。 注意:Sp_attach_db系統(tǒng)存儲(chǔ)過程中只能作用于那些已經(jīng)用Sp_detach_db系統(tǒng)存儲(chǔ)過程從服務(wù)器中拆分出來的數(shù)據(jù)庫。
例6-16:移動(dòng)數(shù)據(jù)庫mytest 到E:\SQL Data 目錄下。 (1) 在SQL Server Query Analyzer 中運(yùn)行系統(tǒng)存儲(chǔ)過程Sp_helpdb, 得到mytest數(shù)據(jù)庫所包含的文件名稱、數(shù)量、類型、存放位置等信息。命令語句如下: exec sp_helpdb mytest

(2) 在SQL Server Query Analyzer 中運(yùn)行Sp_detach_db 系統(tǒng)存儲(chǔ)過程,拆分mytest數(shù)據(jù)庫。命令語句如下: exec sp_detach_db mytest, true
運(yùn)行結(jié)果如下: Successfully detached database 'mytest1'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
(3) 在操作系統(tǒng)的文件夾窗口中用剪切或粘貼的方式直接將與數(shù)據(jù)庫相關(guān)的操作系統(tǒng)文件移動(dòng)到E:\ SQL Data 2000 Server 目錄下。即將D:\SQL Data\mytest_Data.MDF 文件和D:\SQLData\mytest_Log.LDF 文件移到E:\ SQL Data 2000
(4) 在SQL Server Query Analyzer 中運(yùn)行Sp_attach_db 系統(tǒng)存儲(chǔ)過程,附加文件到mytest 數(shù)據(jù)庫。命令語句如下: exec sp_attach_db @dbname = 'mytest', @filename1 = 'e:\sql data\mytest_data.mdf', @filename2 = 'e:\sql data\mytest_log.ldf'
運(yùn)行結(jié)果如下: Successfully attached database 'mytest'. 至此已完成了數(shù)據(jù)庫的移動(dòng)工作,可在Enterprise Manager 中查看mytest 數(shù)據(jù)庫的信息,也可以在SQL Server Query Analyzer 中運(yùn)行系統(tǒng)存儲(chǔ)過程Sp_helpdb 來查看移動(dòng)是否成功。 |