• 用Sql语句还原,分离,删除数据库连接


    项目上线,有问题无法在本机上调试,只能把服务器上的数据库拿下来,在本机调试,但就要还原本机数据库,但在还原的时候,基本不太可能一步到位,因为数据库还在使用当中。

    于是用最无奈的办法:先是分离数据库,并删除连接,然后附加数据库,再还源数据库,今天哥怒了,程序员要做这么麻烦的事,太没水平了!

    于是想办法找了一下sql语句,形成最终解决方案

    第一步:删除连接


    网上的方法:创建一个存储过程,然后调用,可以删除连接

    USE [master]
    GO
    /****** Object:  StoredProcedure [dbo].[p_killspid]    Script Date: 07/29/2010 10:58:55 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER   proc   [dbo].[p_killspid] 
    @dbname   varchar(200)    --要关闭进程的数据库名 
    as     
    declare   @sql     nvarchar(500)     
    declare   @spid   nvarchar(20

    declare   #tb   cursor   for 
    select   spid=cast(spid   as   varchar(20))   from   master..sysprocesses   where   dbid=db_id(@dbname
    open   #tb 
    fetch   next   from   #tb   into   @spid 
    while   @@fetch_status=0 
    begin     
    exec'kill   '+@spid
    fetch   next   from   #tb   into   @spid 
    end     
    close   #tb 
    deallocate   #tb 

     我的方法类似:直接运行的,懒得创建存储过程,放在一个文件里,每次运行,差不多!(查询的位置有些区别,我的数据库sql server 2008)

    use master
    go
    declare @dbName nvarchar(50)
    set @dbName='Test' --数据库名
    declare   @spid   nvarchar(20
    declare   cur_lock   cursor   for 
    SELECT DISTINCT request_session_id FROM master.sys.dm_tran_locks WHERE resource_type = 'DATABASE' AND resource_database_id = db_id(@dbName)
    open   cur_lock 
    fetch   cur_lock      into   @spid 
    while   @@fetch_status=0 
        
    begin     
        
    exec'kill '+@spid
        
    fetch   Next From cur_lock into @spid
        
    end     
    close   cur_lock
    deallocate   cur_lock

    第二步还原,这步非常多参数,一开始根本看不明白,寒

    RESTORE DATABASE [Test] 
        
    FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\P_backup_2010_07_28_180449_1980752.bak' 
        
    WITH  FILE = 1,  
        MOVE N
    'P' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\Test.mdf',  
        MOVE N
    'P_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\Test.ldf',  
        NOUNLOAD,  
        
    REPLACE,  
        STATS 
    = 10

      要说的是,那个Move的参数,指的是备份集中的数据库名,而不是现有的!To的参数是指现有的文件的位置!仔细看看还是可以理解的

     附:分离数据,也是要先删除连接才可以

    EXEC master.dbo.sp_detach_db @dbname = N'Test'
  • 相关阅读:
    virtualBox下面安装linux系统如何共享目录
    PHP中spl_autoload_register()函数
    PHP 5.5 新特性
    useradd密码无效
    Linux audit安全审计工具
    Javascript class获取回调函数数据
    RPi 3B 无线连接配置
    Refused to execute inline event handler because it violates the following Content Security Policy directive: "xxx". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...')
    options.html:1 Refused to load the script 'xxxx' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
    jQuery.Deferred exception: $.get is not a function TypeError: $.get is not a function
  • 原文地址:https://www.cnblogs.com/szyicol/p/1787699.html
Copyright © 2020-2023  润新知