dunitian

汇总篇: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\'

 

分类:

技术点:

相关文章: