--=============================================================
SET NOCOUNT ON
DECLARE @sql nvarchar(max)
DECLARE @sql_move nvarchar(3000)
DECLARE @move nvarchar(200)
DECLARE @backup_type char(1)
DECLARE @name sysname
SET @backup_type = 'L' --N
SET @sql_move = ''
DECLARE cur_restore CURSOR FOR
SELECT name FROM sys.databases WHERE database_id > 4 and NAME != 'LiteSpeedLocal' and state = 0
ORDER BY name
OPEN cur_restore
FETCH NEXT FROM cur_restore
INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = NULL
SET @sql_move = ''
IF @backup_type = 'L'
BEGIN
SET @sql = 'exec master.dbo.xp_restore_database' + char(13)
+ '@database = ''' + @name + '''' + char(13)
+ ',@filename = ''''' + char(13)
+ ',@filenumber = 1' + char(13)
+ ',@with = ''REPLACE''' + char(13)
+ ',@with = ''NORECOVERY''' + char(13)
DECLARE cur_move CURSOR FOR
SELECT ',@with = ''MOVE ''''' +
name + ''''' TO N''''' +
filename + ''''''' '
FROM sys.sysaltfiles WHERE dbid = db_id(@name)
ORDER BY fileid
OPEN cur_move
FETCH NEXT FROM cur_move
INTO @move
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql_move = @sql_move + @move + char(13)
FETCH NEXT FROM cur_move
INTO @move
END
CLOSE cur_move
DEALLOCATE cur_move
END
ELSE IF @backup_type = 'N'
BEGIN
SET @sql = 'RESTORE DATABASE ' + @name + char(13)
+ 'FROM DISK =''''' + char(13)
+ 'WITH NORECOVERY' + char(13)
DECLARE cur_move CURSOR FOR
SELECT ',MOVE ''' + name + ''' TO ''' + filename + ''''
FROM sys.sysaltfiles WHERE dbid = db_id(@name)
ORDER BY fileid
OPEN cur_move
FETCH NEXT FROM cur_move
INTO @move
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql_move = @sql_move + @move + char(13)
FETCH NEXT FROM cur_move
INTO @move
END
CLOSE cur_move
DEALLOCATE cur_move
END
SET @sql = @sql + @sql_move + char(13)
print @sql
FETCH NEXT FROM cur_restore
INTO @name
END
CLOSE cur_restore
DEALLOCATE cur_restore
출처 : http://ceusee.springnote.com/pages/2786844
'연구개발 > DBA' 카테고리의 다른 글
index_cleanup 인덱스 지우기 (0) | 2010.06.15 |
---|---|
Linked Server 쿼리 (0) | 2010.06.11 |
[2005 2008 NF] 크래시 복구, 온라인복원, 미러링 (0) | 2010.05.26 |
[2005 2008 NF] 테이블 및 인덱스 분할 / 동적AWE 메모리 관리 (0) | 2010.05.26 |
[2005 2008 NF] 변경 내용 추적 및 데이터 수집기 (0) | 2010.05.26 |