title: Problem getting the progress status of a SQL-Server restore job
body: <p>I want to use the script from <a href="http://www.wisesoft.co.uk/articles/tsql_backup_restore_progress.aspx" rel="nofollow noreferrer">http://www.wisesoft.co.uk/articles/tsql_backup_restore_progress.aspx</a> to get <strong>progress information</strong> of a <strong>specific running SQL-Server restore job</strong>. To achive this, I have changed it a little bit: </p>
  
  <pre><code>SELECT command,
              s.text,
              start_time,
              percent_complete, 
              CAST(((DATEDIFF(s,start_time,GetDate()))/3600) as varchar) + ' hour(s), '
                    + CAST((DATEDIFF(s,start_time,GetDate())%3600)/60 as varchar) + 'min, '
                    + CAST((DATEDIFF(s,start_time,GetDate())%60) as varchar) + ' sec' as running_time,
              CAST((estimated_completion_time/3600000) as varchar) + ' hour(s), '
                    + CAST((estimated_completion_time %3600000)/60000 as varchar) + 'min, '
                    + CAST((estimated_completion_time %60000)/1000 as varchar) + ' sec' as est_time_to_go,
              dateadd(second,estimated_completion_time/1000, getdate()) as est_completion_time 
  FROM sys.dm_exec_requests r
  CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) s
  WHERE r.command = 'RESTORE DATABASE'  and r.database_id = db_id('database_name')
  </code></pre>
  
  <p>Unfortunately this doesn't work because <strong>r.database_id is never equal to db_id('database_name')</strong>. What is the reason for that? What is wrong with this SQL statement? This script works fine for <strong>'r.command = BACKUP DATABASE'</strong>.</p>
  
  <p>Are there other T-SQL based possibilities to get the progress status of a specific SQL-Server job?</p>