Finding the SQL Server service name and starting it from the command-line
Posted: (EET/GMT+2)
Many developers need SQL Server on their development machines, but it might be that you don't want to allow SQL Server to start automatically when Windows starts. Starting a manual-startup Windows service is easy through the Computer Management GUI, but if you need to do this daily or even several times a day, it can get painful.
Luckily, you could automate the process by writing a simple script to start the service. The Windows command "sc" (short for "service controller") allows you to start (and stop) services. To use the utility, you need to know the name of the service you wish to start.
For instance, the service name for SQL Server 2005 or 2008 is by default "MSSQLSERVER", but changes if you install SQL Server with an instance name. To find the service names on your computer, simply run the command "sc query".
However, the important thing regarding this command is that by default, it only lists running (active) services. This is very easy to forget, and can cause you to scratch your head trying to figure why your service is now listed.
To find all services, is the command "sc query state= all". Note how you need to have a space between the state parameter and its value, "state=all" will not work.
Finally, to find your SQL Service name, use this command:
sc query state= all | find "SERVICE_NAME" |find "SQL"
Oftentimes, the service or instance name is in fully capital letters. Of course, your instance name must have the letters "SQL" in it with correct casing.
Once you have the service name such as "MSSQLSERVER", it's easy to start the service by typing:
sc start MSSQLSERVER
There you have it. And to make things even faster, you could store this command into a batch file, and put it for example on your development machine's desktop. Then it's very convenient to get the services running whenever you need.