Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to find out how to stop / start MS SQL services so that I can copy SQL database files. I know how to do this manually but I would like to do this from a script.

share|improve this question

2 Answers 2

Instead of copying the data files, what about taking a backup copy? That way you don't need to stop/start the SQL Server service and cause downtime, like this for example:

BACKUP DATABASE [databasename] 
TO DISK = 'D:\BackupFiles\Database.bak' 
WITH COPY_ONLY
GO

the COPY_ONLY flag specifies that the backup is (surprise surprise) only a copy, thus not break the differential backup chain.

share|improve this answer
1  
And since you're looking to do this via a script you should execute the above SQL using the "sqlcmd" command prompt tool –  Joel Mansford Sep 3 '12 at 14:45

for stopping:

NET STOP MSSQLSERVER

for starting:

NET START MSSQLSERVER

But i surgest if you want to copy the database files.. (I think for backup purposed).. you might consider using a maintenance plan with a backup schema if you already use MS SQL SERVER. For sql express you need some custom scripts. When the backup of your database is finished, the output file is not locked and you can store that one on a place you like for example a backup device.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.