2010. 6. 3. 23:56

T-SQL::DB_Restore_move_to

  1.  --=============================================================
  2. -- 장비 이동하거나 DB 복원시 파일을 다른 디렉토리로 보관해야할때
  3. -- 파일들이 많은 DB의 경우 일일이 변경하기 어려움이 있어서 원본 기준으로 복원스크립트 생성하고
  4. -- 디렉토리 경로와 백업 파일 경로만 넣어주면 되게끔 생성 한다.
  5. --==============================================================
  6.  

  7. SET NOCOUNT ON

  8. DECLARE @sql            nvarchar(max)

  9. DECLARE @sql_move       nvarchar(3000)

  10. DECLARE @move           nvarchar(200)

  11. DECLARE @backup_type    char(1)

  12. DECLARE @name           sysname

  13. SET @backup_type = 'L' --N

  14. SET @sql_move = ''

  15. DECLARE cur_restore CURSOR FOR

  16.     SELECT name FROM sys.databases WHERE database_id > 4 and NAME != 'LiteSpeedLocal' and state = 0

  17.     ORDER BY name

  18.  

  19. OPEN cur_restore

  20. FETCH NEXT FROM cur_restore

  21. INTO @name

  22.  

  23. WHILE @@FETCH_STATUS = 0

  24. BEGIN

  25.     SET @sql = NULL
        SET @sql_move = ''

  26.     IF @backup_type = 'L'

  27.     BEGIN

  28.         SET @sql = 'exec master.dbo.xp_restore_database' + char(13)

  29.                    + '@database = ''' + @name + '''' + char(13)

  30.                    + ',@filename = ''''' + char(13)

  31.                    + ',@filenumber = 1' + char(13)

  32.                    + ',@with = ''REPLACE''' + char(13)

  33.                    + ',@with = ''NORECOVERY''' + char(13)

  34.  

  35.  

  36.         DECLARE cur_move CURSOR FOR

  37.                 SELECT ',@with = ''MOVE ''''' +

  38.                             name + ''''' TO N''''' +

  39.                             filename + ''''''' '

  40.                 FROM sys.sysaltfiles  WHERE dbid = db_id(@name)

  41.                 ORDER BY fileid

  42.         OPEN cur_move

  43.         FETCH NEXT FROM cur_move

  44.         INTO @move

  45.  

  46.         WHILE @@FETCH_STATUS = 0

  47.         BEGIN

  48.             SET @sql_move = @sql_move + @move + char(13)

  49.            

  50.             FETCH NEXT FROM cur_move

  51.             INTO @move

  52.         END

  53.         CLOSE cur_move

  54.         DEALLOCATE cur_move

  55.  

  56.   

  57.     END

  58.     ELSE IF @backup_type = 'N'

  59.     BEGIN

  60.         SET @sql = 'RESTORE DATABASE ' + @name + char(13)

  61.                  + 'FROM DISK =''''' + char(13)

  62.                  + 'WITH NORECOVERY' + char(13)

  63.  

  64.         DECLARE cur_move CURSOR FOR

  65.                 SELECT ',MOVE ''' + name + ''' TO  ''' + filename + ''''

  66.                 FROM sys.sysaltfiles  WHERE dbid = db_id(@name)

  67.                 ORDER BY fileid

  68.         OPEN cur_move

  69.         FETCH NEXT FROM cur_move

  70.         INTO @move

  71.  

  72.         WHILE @@FETCH_STATUS = 0

  73.         BEGIN

  74.             SET @sql_move = @sql_move + @move + char(13)

  75.            

  76.             FETCH NEXT FROM cur_move

  77.             INTO @move

  78.         END

  79.         CLOSE cur_move

  80.         DEALLOCATE cur_move

  81.     END

  82.  

  83.     SET @sql = @sql + @sql_move + char(13)

  84.  

  85.     print @sql

  86.  

  87.     FETCH NEXT FROM cur_restore

  88.     INTO @name

  89. END

  90.  

  91. CLOSE cur_restore

  92. DEALLOCATE cur_restore

  93.  

이 글은 스프링노트에서 작성되었습니다.

'T-SQL' 카테고리의 다른 글

데이터베이스 사이즈  (0) 2010.06.04
T-SQL::Removing Duplication Data  (1) 2010.06.03
T-SQL::Convert hex value to String 32bit  (0) 2010.06.03
T_SQL::CONSTRAINT조사  (1) 2010.06.03