进程ID“x”不是活动进程ID

时间:2017-05-10 20:49:40

标签: sql tsql sql-server-2008-r2

我需要将trans日志文件恢复到每1小时处于待机模式的DBrestore数据库中。所以我创建了一个工作,它首先杀死所有进程,然后将日志文件恢复到数据库中,但有时作业失败时给出错误:进程ID'71'不是活动进程ID,进程ID每次都更改它失败。 我正在使用此查询来终止进程

declare @sql as varchar(20), @spid as int
select @spid = min(spid)  from master..sysprocesses  
 where dbid = db_id('DBrestore') 
 and spid != @@spid    

while (@spid is not null)
begin
print 'Killing process ' + cast(@spid as varchar) + ' ...'
set @sql = 'kill ' + cast(@spid as varchar)
exec (@sql)

select 
    @spid = min(spid)  
from 
    master..sysprocesses  
where 
    dbid = db_id('DBrestore') 
    and spid != @@spid
end 

我需要在此查询中进行哪些更改,以确保它只接受处于活动状态的进程ID。

提前致谢

1 个答案:

答案 0 :(得分:3)

在下面链接中提供的文档中,它提到通过状态捕获“活动”进程。如果将其添加到where条件,则应将查询限制为活动进程。

declare @sql as varchar(20), @spid as int
select @spid = min(spid)  from master..sysprocesses  
 where dbid = db_id('DBrestore') 
 and spid != @@spid    

while (@spid is not null)
begin
print 'Killing process ' + cast(@spid as varchar) + ' ...'
set @sql = 'kill ' + cast(@spid as varchar)
exec (@sql)

select 
    @spid = min(spid)  
from 
    master..sysprocesses  
where 
    dbid = db_id('DBrestore') 
    and spid != @@spid
    and status = 'running'
end 

https://docs.microsoft.com/en-us/sql/relational-databases/system-compatibility-views/sys-sysprocesses-transact-sql