Reset accurrence count from all SQL Alerts


use msdb
go
set nocount on;

declare @reset nvarchar(1000)
declare @alert nvarchar(128)
declare @occurrence_count int

declare AlertResestCursor cursor
for 
select concat('EXECUTE msdb.dbo.sp_update_alert @name = N''',name,''', @occurrence_count = 0;') , name, occurrence_count
from [msdb].[dbo].[sysalerts]
where occurrence_count > 0
open AlertResestCursor
fetch next from AlertResestCursor into  @reset, @alert, @occurrence_count

while @@FETCH_STATUS = 0 
begin 
	print concat('Alert [', @alert , '] has been reseted from ', @occurrence_count, ' to 0')
	exec (@reset)
	fetch next from AlertResestCursor into @reset, @alert, @occurrence_count
end
CLOSE AlertResestCursor;  
DEALLOCATE AlertResestCursor;