匯總篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql
某系統設計的不是很合理,庫很多,圖形化操作分離都得搞半天,各種改名也就更浪費時間了,於是引入了命令~(SQLServer現在已經在Linux里面跑了,咱們也得跟上時代)
1.數據庫名修改前
alter database Test modify name=NewTest or exec sp_renamedb 'Test','NewTest'
2.數據庫名修改后
3.物理文件名和邏輯名並沒有變化
4.邏輯名修改前后
alter database NewTest modify file(name=N'Test', newname=N'NetTest')
5.邏輯名發生改變物理文件名不變
6.物理改名很多種(我這邊的本質就是分離后修改,因為占用狀態是沒法修改的)
其實並沒有什么新的sql,都是組合版的
exec xp_cmdshell 'rename E:\SQL\Test.mdf NewTest.mdf'
效果:
SQL:
use master go --1.分離 exec sp_detach_db NewTest go --2.改名(這一步可以換成手動改名字) exec sp_configure 'show advanced options',1 --顯示高級選項 reconfigure with override--重新配置 exec sp_configure 'xp_cmdshell',1 --1代表允許,0代表阻止 reconfigure with override exec xp_cmdshell 'rename E:\SQL\Test.mdf NewTest.mdf' go exec xp_cmdshell 'rename E:\SQL\Test_log.ldf NewTest_log.ldf' go exec sp_configure 'xp_cmdshell',0 reconfigure with override exec sp_configure 'show advanced options',0 reconfigure with override --3.附加 exec sp_attach_db NewTest,N'E:\SQL\NewTest.mdf',N'E:\SQL\NewTest_log.ldf'