Wednesday, 15 November 2017

How to restore database by using T-SQL Scripts

Hi All,  

Today I am going to explain how to restore database by using T-SQL Scripts. We already have database in sql server and database backup file .

DECLARE @DbName nvarchar(50)
SET @DbName = N'TestDB'
DECLARE @EXECSQL varchar(max)
SET @EXECSQL = ''
SELECT @EXECSQL = @EXECSQL + 'Kill ' + Convert(varchar, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(@DbName) AND SPId  =@@SPId
EXEC(@EXECSQL)

ALTER DATABASE [TestDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

RESTORE DATABASE [TestDB] FROM DISK = 'D:\backup\TestDB.bak' WITH REPLACE 
GO

Thanks for reading. :) 

No comments:

Post a Comment