Cari Blog Ini

13 Juni 2012

Example of Restore Database with Attach and Detach Database Using Syntax SQL

/* Task for : Detach DB */
--------------------------------------------------------------------
-----code:start
--------------------------------------------------------------------
USE [master]
GO

ALTER DATABASE [DB_Test]
    SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

USE [master]
GO

EXEC master.dbo.sp_detach_db @dbname = N'DB_Test'
GO
--------------------------------------------------------------------
-----code:end
--------------------------------------------------------------------


/* Task for : Attach DB */
----------------------------------------------------
-----code:start
----------------------------------------------------
USE [master]
GO

CREATE DATABASE [DB_Test] ON
    ( FILENAME = N'C:\DBTest\DB_Test.mdf' ),
    ( FILENAME = N'C:\DBTest\DB_Test_log.ldf' )
     FOR ATTACH
GO
----------------------------------------------------
-----code:end
----------------------------------------------------


/* Task for : Restore Database */
----------------------------------------------------
-----code:start
----------------------------------------------------
RESTORE DATABASE [DB_Test]
FROM  DISK = N'\\xxx.xxx.x.44\Share\RestoreDB\DB_Test_120613_1340.DAT'
    WITH  FILE = 1,
   
    MOVE N'DB_Test'
    TO N'C:\DBTest\DB_Test.mdf', 
   
    MOVE N'DB_Test_Log'
    TO N'C:\DBTest\DB_Test_Log.ldf',     
   
    NOUNLOAD, 
    REPLACE, 
    STATS = 10
GO
----------------------------------------------------
-----code:end
----------------------------------------------------


Results Messages:
14 percent processed.
24 percent processed.
34 percent processed.
44 percent processed.
54 percent processed.
64 percent processed.
74 percent processed.
84 percent processed.
94 percent processed.
100 percent processed.
Processed 160 pages for database 'DB_Test', file 'DB_Test' on file 1.
Processed 1 pages for database 'DB_Test', file 'DB_Test_log' on file 1.
RESTORE DATABASE successfully processed 161 pages in 0.151 seconds (8.329 MB/sec).