• sql 数据库还原脚本 (kill链接+独占


      在开发过程中经常会碰到数据库还原,要是sql 连接没完全释放掉,那么还原就会受到阻碍。此脚本就是为了解决这个问题。




    USE
    [master] GO /****** Object: StoredProcedure [dbo].[sp_backrecovery] Script Date: 03/12/2014 09:42:38 ******/ IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_backrecovery]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[sp_backrecovery] GO USE [master] GO /****** Object: StoredProcedure [dbo].[sp_backrecovery] Script Date: 03/12/2014 09:35:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --执行自动还原 /* exec [master].[dbo].sp_backrecovery 'D:SQLSERVERackuiaa','uiaa_test' */ create procedure [dbo].[sp_backrecovery] ( @Path nvarchar(260), @dbname varchar(200) ) as begin if RIGHT(@Path, 1) <> '' set @Path = @Path + '' declare @back table( id int identity, directory nvarchar(260), depth int, IsFile bit ) insert @back exec master.dbo.xp_dirtree @path = @path, @depth = 0, @file = 1 DECLARE @depth int, @depthMax int UPDATE @back SET directory = @Path + directory WHERE depth = 1 SELECT @depth = 2, @depthMax = MAX(depth) FROM @back WHILE @depth <= @depthMax BEGIN UPDATE A SET directory = ( SELECT TOP 1 directory FROM @back WHERE depth = @depth - 1 AND IsFile = 0 AND id < A.id ORDER BY id DESC ) + N'/' + directory FROM @back A WHERE depth = @depth SET @depth= @depth + 1 END --kill 连接 declare @kills table ( id int identity(1,1), spid varchar(10) ) insert into @kills select spid from sysprocesses where dbid=db_id(@dbname) declare @i int declare @icount int set @i=1 select @icount=count(1) from @kills while @i<=@icount begin declare @spid varchar(10) select @spid=spid from @kills where id=@i declare @killsql varchar(200) set @killsql='kill '+ @spid exec (@killsql) set @i=@i+1 end --完整备份恢复 declare @filepath varchar(1000) select top 1 @filepath=directory from @back order by directory desc declare @sql varchar(1000) set @sql='alter database '+@dbname+' set single_user ' exec (@sql) set @sql=' restore database '+@dbname+' from disk ='''+@filepath+''' with replace,recovery' exec (@sql) set @sql=' alter database '+@dbname+' set multi_user ' exec (@sql) end
  • 相关阅读:
    Linux(centos7)安装maven3.5
    mysql安装错误之->ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    CentOS7 安装tomcat为系统服务器 Systemctl管理Tomcat,并设置开机启动
    linux(centos)设置tomcat开机启动
    linux(centos7)安装tomcat
    修改Tomcat8w.exe可执行路径:Path to executable
    CentOS 7更改yum源与更新系统
    最全的PHP开发Android应用程序
    Cookies的各方面知识(基础/高级)深度了解
    使用Hash直接登录Windows(HASH传递)
  • 原文地址:https://www.cnblogs.com/zhuwenjun/p/3595679.html
Copyright © 2020-2023  润新知