Search Chirayu Software Solutions Blog

Thursday, July 17, 2014

To Attach a Database File using sqlcmd command prompt

To Attach a Database File

  1. Open the command prompt on the server.
  2. From the command prompt, connect to an instance of SQL Server by using the following sqlcmd command:
    sqlcmd -S Server\Instance
    
    Where Server is the name of the computer and Instance is the name of the instance.
  3. When connected, type the following commands:
    USE [master]
    GO
    CREATE DATABASE [database_name] ON 
    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\<database name>.mdf' ),
    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\<database name>.ldf' )
     FOR ATTACH ;
    GO
    
    Where database_name is the name of the database you want to attach, FileName is the path and filename of the database file and the log file, and FOR ATTACH specifies that the database is created by attaching an existing set of operating system files.
  4. To verify that the database has been attached, type the following two commands:
    select name from sys.databases
    go
Post a Comment