title: Process id 'x' is not an active process ID
body: <p>I need to restore trans log files into  database DBrestore which is in standby mode for every 1 hour . So I created a job where it kills all the process first and then restores the  log files into the database,  but sometimes the job  fails giving the error as : Process id '71' Is not an active process ID, the process id changes every time  it fails. 
  Im using this query to kill the process</p>
  
  <pre><code>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 
  </code></pre>
  
  <p>What changes do I need to make in this query to make sure it only takes the process id which is in active. </p>
  
  <p>Thanks in advance </p>