Start and Stop SQL Server from Windows Command Line or File

Seeing as how I was given a regular workstation rather than a developer's workstation at my new job, RAM is at a premium. It's amazing what you can still accomplish with 4 GB but you have to be judicious in its use.

Towards that end, I'm running the local instance of SQL Server on an as-needed basis. Rather than opening Services from the Windows Start menu's "Administrative Tools" item, I created two cmd files (and, yes, I should've used PowerShell): one to start the database engine instance and a second to stop the database engine instance.

StartSql.cmd
NET START MSSQL$SQLEXPRESS2008R2
SET /p DUMMY=Hit ENTER to continue...

StopSql.cmd
NET STOP MSSQL$SQLEXPRESS2008R2
SET /p DUMMY=Hit ENTER to continue...

Once I had the cmd files done, I went into Services and updated the SQL Server service for that instance so that it was manually started rather than automatically started. Finally, I placed a shortcut for each cmd file on my desktop.

Now, when I want to fire up the database, I right-click on the shortcut and select "Run as administrator" from the popup menu. Note that there's an option, if you right-click on the shortcut and open up the properties, to go to the Compatibility tab and check a box for "Run this program as an administrator." For me, this box was grayed out and not available to check. YMMV.

For a list of commands, visit this page, https://technet.microsoft.com/en-us/magazine/dd421654.aspx, from the TechNet website.

Note that there's a lot more could do with those command file but the essentials are what you see above. Also, you could go all sorts of crazy with PowerShell and build something pretty robust. I only needed to start and stop the service and see if the start or stop was successful (thus the SET command), so my files are very simple.

Comments

Popular posts from this blog

Using Reference Aliases

List of Visual Studio Keyboard Shortcuts