请教如何根据SqlServerAgent 服务当前状态进行操作
SqlServerAgent 服务开机自动启动,然后执行EXEC master.dbo.xp_servicecontrol N'START',N'SQLSERVERAGENT' 出现StartService() 返回错误 1056,“服务的范例已在运行中。”
请教,应如何根据SqlServerAgent 服务当前状态再执行启动服务语句,就是先查询SqlServerAgent 服务的当前状态(EXEC master.dbo.xp_servicecontrol N'QUERYSTATE',N'SQLSERVERAGENT'),如为Running,就不执行启动服务语句,否则执行启动服务语句。谢谢!!
[解决办法]
DECLARE @temp TABLE([state] NVARCHAR(255))
INSERT @temp([state])
EXEC master.dbo.xp_servicecontrol N'QUERYSTATE',N'SQLSERVERAGENT'
IF(SELECT TOP(1) [state] FROM @temp) <> 'Running.'--Running./Stopped.
BEGIN
EXEC master.dbo.xp_servicecontrol N'START',N'SQLSERVERAGENT'
END
IF object_id('tempdb..#temp', 'u') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp ([state] VARCHAR(255))
DECLARE @sql VARCHAR(1000)
SET @sql = '
INSERT INTO #temp([state])
EXEC master.dbo.xp_servicecontrol N''QUERYSTATE'',N''SQLSERVERAGENT''
'
EXEC (@sql)
IF(SELECT TOP(1) [state] FROM #temp) <> 'Running.' --Running./Stopped.
BEGIN
EXEC master.dbo.xp_servicecontrol N'START',N'SQLSERVERAGENT'
END