Sunday, May 31, 2009

How to attach and detach SQL Server database by command line

SQL Server comes with an useful command line tool (sqlcmd.exe) to help manage your database. And here is how to attach a database to SQL Server:

C:\>sqlcmd -U your_username -P your_password
1> use msdb
2> go
Changed database context to 'msdb'.
1> sp_attach_db 'database_name', 'data_file', 'log_file'
2> go

Update: And here is how to detach a database by command line

C:\>sqlcmd -U your_username -P your_password
1> use msdb
2> go
Changed database context to 'msdb'.
1> sp_detach_db 'database_name'
2> go

No comments: