Restore DB in SQL Server 2005

I sepnt more hours in internet and finally i got the reliable script through which i easily restore the database.

Here is the script:



USE MASTER
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[$(database)]') AND type in (N'U'))
ALTER DATABASE $(database) SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

create table #backupInformation (LogicalName varchar(100),
PhysicalName varchar(100),
Type varchar(1),
FileGroupName varchar(50) ,
Size bigint ,
MaxSize bigint,
FileId int,
CreateLSN int,
DropLSN int,
UniqueId uniqueidentifier,
ReadOnlyLSN int,
ReadWriteLSN int,
BackupSizeInBytes int,
SourceBlockSize int,
FileGroupId int,
LogGroupGUID uniqueidentifier,
DifferentialBaseLSN bigint,
DifferentialBaseGUID uniqueidentifier,
IsReadOnly bit, IsPresent bit )

insert into #backupInformation exec('restore filelistonly from disk = ''$(filepath)''')

DECLARE @logicalNameD varchar(255);
DECLARE @logicalNameL varchar(255);

select top 1 @logicalNameD = LogicalName from #backupInformation where Type = 'D';
select top 1 @logicalNameL = LogicalName from #backupInformation where Type = 'L';

DROP TABLE #backupInformation

RESTORE DATABASE $(database)
FROM DISK = '$(filepath)'
WITH REPLACE,
MOVE @logicalNameD TO '$(mdffilelocation)\$(database).mdf',
MOVE @logicalNameL TO '$(ldffilelocation)\$(database).ldf'
GO


you need to change $(database) with your database name, $(filepath) with your db file path where it is,$(mdffilelocation) with appropriate MDF file location,

$(ldffilelocation) with appropriate LDF file location.

Thats it.


Happy Coding....

No comments:

Post a Comment