现在我要作数据库远程备份,与恢复,恢复时,希望先把远程SQL服务先停止,再启动。可是停止后我不知道如何在Delphi里启动远程SQL服务?

解决方案 »

  1.   

    : zjcxc(邹建) ( ) 信誉:316 
    /*
    关闭用户打开的进程处理
    */
    use master
    goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_killspid]
    GOcreate proc 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
    go--用法  
    exec p_killspid  '数据库名'
    =======================================================
    用的delphi执行上面的脚本之后,在使用restore命令恢复,不用关掉sql服务
      

  2.   

    可以解释,一下吗,说实话我都不知道为什么大家建议再恢复之前先关闭sql服务,再启动?
    还有你上面的脚本是什么意思,有什么作用,谢谢
      

  3.   

    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--逐个将进程kill掉,这个个时候所有的客户端都强制与服务器断开连接,启动restor 命令的时候就不回报数据库在使用的错误了===========================================可以的,你再试试
      

  4.   

    关键在use master,然后 exec p_killspid xxxx(数据库名),这是没有冲突的,他在master数据库运行过程去结束所有 xxxx数据库的进程,resotre的时候也只是resotre xxxx数据库