SQL Server - Move a database manually via SQL

To list the logical files in a sql server database in interactive OSQL sessions, try this:

Use mydb;
go
select * from sysfiles;
go

You will see a list of system files, including the names for log and data files. This is required information if you wish to run this command to shrink a log file that has grown too big for some reason:

dbcc shrinkfile([LOGICAL NAME OF LOG FILE IN DATABASE],X)

 

For example, to move a database to a new computer: 

sp_helpfile; -- this lists the files

sp_detach_db 'testdb'; -- detach, you have to be using a different DB (you can not detach the database that you are on

sp_attach_db 'testdb', 'e:\data\testdb.mdf', 'e:\data\testdblog.ldf'; -- re-attach on new computer

 


Source: http://www.blackwasp.co.uk/MoveSQLDatabase.aspx


tags: sql, files, migrate, move, osql, raw, mssql, tsql, osql

Related Scribbles:
  • List Databases MSSQL
  • Quickly Shrink SQL Server Transaction Logs
  • SQL Server


  • ID: 735
    Author:
    leonard
    Date Updated:
    2010-09-16 14:59:20
    Date Created:
    2006-01-05 23:20:07

    Edit

    Comments?
     >> Leonard Chan's Homepage  >> Scribble Web  >> SQL Server - Move a database manually via SQL
    leonard.lotus-land.ca is hosted by Perceptus Solutions Inc.