Q:

How to restore .bak only if it's good

I'm using Tsql script to restore several databases.
RESTORE DATABASE XXX_M
   FROM DISK = 'C:\XXX.bak'
   WITH FILE = 1, replace
GO

Sometimes the .bak file comes corrupted (not data corrupted ).
I want to know if there is a way to check if the file is not corrupted then restore else don't.
I've tried to use @@rowcount after restore VERIFYONLY but it's not good.
RESTORE VERIFYONLY 
   FROM DISK = 'C:\xxx.bak'
if @@ROWCOUNT !=0
print 'false'
else 
print 'good'

Any other ideas?
Thanks.

A:

Checking @@ROWCOUNT isn't valuable here because RESTORE VERIFONLY doesn't return any results. It either prints status messages or error messages. So, a simple TRY/CATCH should do:
BEGIN TRY
  RESTORE VERIFYONLY ...
  RESTORE DATABASE   ...
END TRY
BEGIN CATCH
  PRINT ERROR_MESSAGE();
END CATCH

If the VERIFYONLY fails, it will raise an exception, and skip the RESTORE.
Of course, there are still going to be scenarios where VERIFYONLY will succeed but the RESTORE will fail (for example, something swoops in and takes all your disk space).

