Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Thursday, 23 July 2026

How to See When Your SQL Server Backup or Restore Will Finish

 

If you've ever started a database backup or restore and then sat there wondering how much longer it'll take, you already know the problem. A large database can leave you staring at a spinning cursor with no idea whether you're waiting one more minute or another hour.



The good news is that when a backup or restore is running, SQL Server already knows roughly when it expects to finish. You just have to ask for it. This query does exactly that, and the column that matters most is estimated_completion_time.

Run this in a new query window while your backup or restore is going, and you'll get a row back for each active operation, along with a real clock time for when SQL Server expects to be done.


The script

SELECT session_id as SPID, command, a.text AS Query, start_time, percent_complete, dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a WHERE r.command in ('BACKUP DATABASE','RESTORE DATABASE')



The star of the show: estimated_completion_time

This is the column that turns a nerve-wracking wait into something you can actually plan around.

Inside sys.dm_exec_requests, SQL Server exposes an estimated_completion_time value, but the raw number is the estimated milliseconds remaining. On its own, that's not very useful. Nobody wants to do mental math on "4,380,000 milliseconds" to figure out when they can go to lunch.

That's what this part of the query fixes:

dateadd(second, estimated_completion_time/1000, getdate())

It divides the milliseconds by 1000 to get seconds, then adds those seconds to the current time with getdate(). The result is an actual finish time, like 2026-07-23 14:52:10, instead of a raw count you have to interpret. You look at it and immediately know when the operation should wrap up.

The key thing is that this value is live. SQL Server recalculates it as the operation runs, so each time you execute the query you get a fresh estimate based on the work done so far. Run it, wait a bit, run it again, and you'll watch the projected finish time settle down as the operation progresses. That's your progress, right there in SSMS, without any external tool.

The percent_complete column tells you how far along you are; the estimated completion time tells you the one thing people actually care about, which is when this will be done.


You'll also need VIEW SERVER STATE permission to see this, since it reads from sys.dm_exec_requests and sys.dm_exec_sql_text. Most DBAs already have it, but a restricted login might not.

And if you want a running view, just keep hitting execute every few seconds. Watching that projected finish time creep closer is a lot less stressful than staring at a blank screen wondering if anything is happening at all.


Wrapping up

This is one of those small scripts worth keeping in your snippets folder. When you're running a backup or restore, it lets you see the progress directly in SQL Server and, more importantly, tells you when the operation will actually finish, so you can plan around it instead of guessing.



Friday, 5 December 2014

Restore fails with error "The media set has 2 media families but only 1 are provided. All members must be provided."

I was trying to restore a SQL backup (.bak) file of NAV2013R2 and got this error message.



The media set has 2 media families but only 1 are provided. All members must be provided.

Msg 3132, Level 16, State 1, Line 1 
The media set has 2 media families but only 1 are provided. All members must be provided. 
Msg 3013, Level 16, State 1, Line 1 
RESTORE DATABASE is terminating abnormally.


I tried many ways but still got same error.

I tried to take backup again to check whether I can find anything strange.
I noticed having 2 Destination files added while taking backup.



This error is due to your backup being divided into more than one part at the time of creating your backup file.

Remove one path from the backup destination so that only one .bak file is created.
Now I was able to restore the backup file successfully.

Friday, 21 March 2014

Reset Autoincrement field in NAV

In SQL Server we have command called DBCC CHECKIDENT 

Checks the current identity value for the specified table and, if needed, corrects the identity value.
You can also use DBCC CHECKIDENT to manually set a new current identity value for the identity column.

The syntax of the DBCC CHECKIDENT command is as follows:

DBCC CHECKIDENT ( <table_name> [ , { NORESEED | { RESEED [, <new_reseed_value> ] } } ] )
[ WITH NO_INFOMSGS ]

The <table_name> parameter is the name of the table for which to check the current identity value and it must contain an identity column.  The NORESEED clause specifies that the current identity value should not be changed.  The RESEED clause specifies that the current identity value should be changed.  The <new_reseed_value> is the new value to be used as the current value of the identity column.  Lastly, the WITH NO_INFOMSGS clause suppresses all informational messages.


If the table has no rows and execute below statement

Use [Demo Database NAV (7-1)]
GO
DBCC CHECKIDENT ([CRONUS Nederland BV$Test Increment], NORESEED);
GO

Result:
Checking identity information: current identity value 'Null', current column value 'Null'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

If it has one row then result of above statement is:

Checking identity information: current identity value '1', current column value '1'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.


To set the IDENTITY column of a table to start from 1, the following statement can be issued:
The following example forces the current identity value in the table to a value of 0. The next row inserted will use 1 as the value, that is, the new current increment value defined for the column value plus 1.

Use [Demo Database NAV (7-1)]
GO
DBCC CHECKIDENT ([CRONUS Nederland BV$Test Increment], RESEED,0);
GO

Permissions:
Caller must own the table, or be a member of the sysadmin fixed server role, the db_owner fixed database role, or the db_ddladmin fixed database role.