Tuesday, 12 January 2016

How to Restore the Database with code

Hi Guys,

This is the following command to restore the Database with given .bak file.

DECLARE @fileList TABLE (backupFile NVARCHAR(255))
DECLARE @lastFullBackup NVARCHAR(500)
DECLARE @cadpath NVARCHAR(500)

INSERT INTO @fileList(backupFile)
EXEC xp_cmdshell 'dir C:\AX-Databases\*.bak'


select @lastFullBackup = substring(MAX(backupFile),CHARINDEX('H', MAX(backupFile)),8000) from @fileList
WHERE backupFile LIKE '%.bak'
AND backupFile LIKE '%'+'test' + '%'

set @cadpath = 'C:\AX-Databases\' + @lastFullBackup
RESTORE DATABASE test_Live FROM DISK = @cadpath



Vivek Chirumamilla

No comments:

Post a Comment