Tuesday, March 8, 2016

Disable or enable alerts , change all alerts notification Operator

--Check All Alerts:
SELECT * from msdb.[dbo].[sysalerts]


 --Disable All Alerts
 UPDATE msdb.[dbo].[sysalerts]
SET [enabled] = 0
WHERE [enabled] = 1


  --Enable All Alerts
 UPDATE msdb.[dbo].[sysalerts]
SET [enabled] = 0
WHERE [enabled] = 1



---Change Alerts Notify operators
SET NOCOUNT ON
DECLARE @Alert_Names TABLE
(
AlertName SYSNAME NOT NULL,
Operator_name Varchar(30) NULL
)
Declare @operator_name Varchar(30)='SQLOperDBA'
 Declare @notification_method int = 1 ;
INSERT INTO @Alert_Names(AlertName,Operator_name)
SELECT s.name,@operator_name
FROM msdb.[dbo].[sysalerts] s
WHERE s.Enabled = 0 --Optional filter
ORDER BY s.name

DECLARE @Alert_name SYSNAME
DECLARE @Alert_id UNIQUEIDENTIFIER


DECLARE ChangeOperator CURSOR FOR
SELECT Alertname,operator_name
FROM @Alert_Names


OPEN ChangeOperator
FETCH NEXT FROM ChangeOperator INTO @Alert_name,@operator_name

WHILE @@FETCH_STATUS = 0
BEGIN

EXEC msdb.dbo.sp_add_notification  @alert_name, @operator_name,1

FETCH NEXT FROM ChangeOperator INTO @Alert_name,@operator_name

END

CLOSE ChangeOperator
DEALLOCATE ChangeOperator

No comments:

Post a Comment

https://blog.sqlauthority.com/2009/06/27/sql-server-fix-error-17892-logon-failed-for-login-due-to-trigger-execution-changed-database-context...