måndag 19 maj 2008

How to start an SQlserver Agent from a Script.

Run this script in a query window agains Master DB, and if you have the rights to execute this SP (xp_servicecontrol) then it will start the sqlserverAgent even if you dont have access to SQlServer Configuration Manager. Or the server via Remote.

------------------------------------------------------------------------------------
DECLARE @Err int, @Msg varchar(100), @ServiceName sysname
SET @ServiceName = 'SQLServerAgent'

EXEC master.dbo.xp_servicecontrol 'START', @ServiceName

SET @Err = @@ERROR
IF @Err = 0

BEGIN
RAISERROR ('Successfully started SQL Server Agent', 1, 1) WITH LOG
END
ELSE
BEGIN
SET @Msg = 'Error occured while starting SQL Server Agent. Error code: ' + STR(@Err) RAISERROR (@Msg, 18, 1) WITH LOG
END
END

GO
------------------------------------------------------------------------------------

For startin the SqlServerAgent automatically when it goes down:

EXEC sp_procoption 'AutoStart_SQLAgent', 'startup', 'true'GO

------------------------------------------------------------------------------------

Inga kommentarer: