가끔 들어 오는 업무 중에 시점 복원을 해야 하는 경우가 있다. 이럴 경우 trn 백업 데이터를 다 적어
줘야 한다. 하지만 이 쿼리를 통해 스크립트로 복원 스크립트를 자동으로 생성해 낸다.
사용 하면 편할듯~~~~~~~
DECLARE
@databaseName
sysname
DECLARE
@backupStartDate
datetime
DECLARE
@backup_set_id_start
INT
DECLARE
@backup_set_id_end
INT
-- set database to be used
SET
@databaseName
=
'복원할DB명'
SELECT
@backup_set_id_start
= MAX
(
backup_set_id
)
FROM
msdb.dbo.backupset
WHERE
database_name
=
@databaseName
AND
type
=
'D'
SELECT
@backup_set_id_end
= MIN
(
backup_set_id
)
FROM
msdb.dbo.backupset
WHERE
database_name
=
@databaseName
AND
type
=
'D'
AND
backup_set_id
>
@backup_set_id_start
IF
@backup_set_id_end
IS
NULL
SET
@backup_set_id_end
=
999999999
SELECT
backup_set_id
,
'RESTORE DATABASE '
+
@databaseName
+
' FROM DISK = '''
+
mf.physical_device_name
+
''' WITH NORECOVERY'
FROM
msdb.dbo.backupset b
,
msdb.dbo.backupmediafamily mf
WHERE
b.media_set_id
=
mf.media_set_id
AND
b.database_name
=
@databaseName
AND
b.backup_set_id
=
@backup_set_id_start
UNION
SELECT
backup_set_id
,
'RESTORE LOG '
+
@databaseName
+
' FROM DISK = '''
+
mf.physical_device_name
+
''' WITH NORECOVERY'
FROM
msdb.dbo.backupset b
,
msdb.dbo.backupmediafamily mf
WHERE
b.media_set_id
=
mf.media_set_id
AND
b.database_name
=
@databaseName
AND
b.backup_set_id
>=
@backup_set_id_start
AND
b.backup_set_id
<
@backup_set_id_end
AND
b.type
=
'L'
UNION
SELECT
999999999
AS
backup_set_id
,
'RESTORE DATABASE '
+
@databaseName
+
' WITH RECOVERY'
ORDER BY
backup_set_id
http://www.mssqltips.com/tip.asp?tip=1243
[원본사이트]
http://www.mssqltips.com/tip.asp?tip=1243