Start bat script sharing of SQL Server Service

Time:2021-5-3

I declare that I didn’t write this script. I forgot where I saw it. I’d like to share it with you, because in my understanding, technology is for sharing. I hope the original author doesn’t mind if he sees it.

1. Create a text and change the suffix to. Bat

2. Paste the following statement and save it

Copy codeThe code is as follows:
@echo off
for /f “skip=3 tokens=4” %%i in (‘sc query MSSQLSERVER’) do set “zt=%%i” &goto :next

:next
if /i “%zt%”==”RUNNING” (
  Echo has found that the service is running and started to stop
 net stop  MSSQLSERVER
  Echo SC config MSSqlServer start = disabled
) else (
  Echo the service is now processing the stopped state and starting
  Echo SC config MSSqlServer start = manual
 net start MSSQLSERVER
)

pause

Note that ‘MSSqlServer’ is the default instance name of SQL server. If you have multiple instances on the server, you need to manually replace the service name in the script.