When you try to restore the database (RESTORE DATABASE
) through command and if it's in use then SQL SERVER will not allow you to do so. It will throw an error.
But there is a way to restore database forcefully. But there are chances of data loss.
To restore database forcefully, one need to bring it in SINGLE USER mode. and then restore it and again put the database in MULTI USER mode. Make sure, you are always using MASTER database for this operation.
See below script.
--To Restore forcefully to an existing database which is in use.
ALTER DATABASE virendra SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE virendra FROM DISK='D:\virendra.bak' WITH REPLACE
ALTER DATABASE virendra SET MULTI_USER
Asked In: Many Interviews |