在移动之前会报错:
sql Server阻止了对组件xp_cmdshell过程的解决方案
错误描述:sql Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问。因为此组件已作为此服务嚣安全配置的一部分而被关闭。系统管理员可以通过使用sp_configure启用‘xp_cmdshell’。有关启用‘xp_cmdshell’的详细信息,请参阅sql帮助文件。
可以在Master下执行
sp_configure 'show advanced options',1reconfiguregosp_configure 'xp_cmdshell',1reconfigurego整理 之后的脚本 , 注意:移动的文件目录必须存在
USE masterexec sp_configure 'show advanced options',1reconfiguregoexec sp_configure 'xp_cmdshell',1reconfiguregoGODECLARE @DBname sysname,@DestPath varchar(256)DECLARE @DB table( name sysname,physical_name sysname)BEGIN TRYSELECT @DBname = 'dbname',--input database name @DestPath = 'e:\db' --input destination path-- kill database processesDECLARE @SPID varchar(20)DECLARE curProcess CURSOR FORSELECT spIDFROM sys.sysprocessesWHERE DB_name(dbID) = @DBnameOPEN curProcess FETCH NEXT FROM curProcess INTO @SPID WHILE @@FETCH_STATUS = 0 BEGIN EXEC('KILL ' + @SPID) FETCH NEXT FROM curProcess ENDCLOSE curProcessDEALLOCATE curProcess-- query physical nameINSERT @DB( name,physical_name)SELECT A.name,A.physical_nameFROM sys.master_files AINNER JOIN sys.databases B ON A.database_ID = B.database_ID AND B.name = @DBnameWHERE A.type <=1--set offlineEXEC('ALTER DATABASE ' + @DBname + ' SET OFFliNE')--move to dest pathDECLARE @login_name sysname,@physical_name sysname,@temp_name varchar(256)DECLARE curMove CURSOR FORSELECT name,physical_nameFROM @DBOPEN curMove FETCH NEXT FROM curMove INTO @login_name,@physical_name WHILE @@FETCH_STATUS = 0 BEGIN SET @temp_name = RIGHT(@physical_name,CHARINDEX('\',REVERSE(@physical_name)) - 1) EXEC('exec xp_cmdshell ''move "' + @physical_name + '" "' + @DestPath + '"''') EXEC('ALTER DATABASE ' + @DBname + ' MODIFY file ( name = ' + @login_name + ',filename = ''' + @DestPath + @temp_name + ''')') FETCH NEXT FROM curMove INTO @login_name,@physical_name ENDCLOSE curMoveDEALLOCATE curMove-- set onlineEXEC('ALTER DATABASE ' + @DBname + ' SET ONliNE')-- show resultSELECT A.name,A.physical_nameFROM sys.master_files AINNER JOIN sys.databases B ON A.database_ID = B.database_ID AND B.name = @DBnameEND TRYBEGIN CATCH SELECT ERROR_MESSAGE() AS ErrorMessageEND CATCHGO总结
以上是内存溢出为你收集整理的SQLserver移动数据库文件全部内容,希望文章能够帮你解决SQLserver移动数据库文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)