Skip to content

Instantly share code, notes, and snippets.

@exemplum100
Created July 16, 2023 07:32
Show Gist options
  • Save exemplum100/cc7de357fa1fcc5d3ce1ecbe22ad8038 to your computer and use it in GitHub Desktop.
Save exemplum100/cc7de357fa1fcc5d3ce1ecbe22ad8038 to your computer and use it in GitHub Desktop.
Пример использования курсора для поиска аномалий в системе для выявления инцидента
CREATE TABLE #rezults
(cobj BIGINT,
times DATETIME,
counts INT)
DECLARE @xg1 DATETIME2,
@xg2 INT,
@xg3 BIGINT,
@mess VARCHAR(300),
@logr DATETIME2
SET @xg2=0
DECLARE react_kursor CURSOR LOCAL FOR
SELECT kr.new1,kr.new2
FROM #forkurs AS kr
ORDER BY kr.new1
OPEN react_kursor
FETCH NEXT FROM react_kursor
INTO @xg1,@xg3
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE @NEWTIME DATETIME2
IF EXISTS(SELECT TOP(1) 1 FROM dbo.new1
WHERE RECEIVED BETWEEN @xg1 AND DATEADD(MINUTE,7,@xg1) AND COBJ_OBJECT_NO=@xg3 AND MESSAGE_TYPE_NO IN (10838215,10838213,10838229,10838250,1580794096,10838227,10838220,10838241,10838232,7884498668))
BEGIN
SET @NEWTIME=(SELECT TOP (1) RECEIVED FROM dbo.new1
WHERE RECEIVED BETWEEN @xg1 AND DATEADD(MINUTE,7,@xg1) AND COBJ_OBJECT_NO=@xg3 AND MESSAGE_TYPE_NO IN (10838215,10838213,10838229,10838250,1580794096,10838227,10838220,10838241,10838232,7884498668))
IF EXISTS(SELECT TOP(1) 1 FROM dbo.new1 WHERE RECEIVED BETWEEN DATEADD(MILLISECOND,1,@NEWTIME) AND DATEADD(MINUTE,10,@NEWTIME) AND COBJ_OBJECT_NO=@xg3 AND MESSAGE_TYPE_NO IN(8970923576,200610999))
BEGIN
SET @logr=(SELECT TOP(1) RECEIVED FROM dbo.new1 WHERE RECEIVED BETWEEN DATEADD(SECOND,10,@NEWTIME) AND DATEADD(MINUTE,10,@NEWTIME) AND COBJ_OBJECT_NO=@xg3 AND MESSAGE_TYPE_NO IN(8970923576,200610999))
IF @xg3 NOT IN(SELECT r.cobj FROM #rezults AS r)
BEGIN
SET NOCOUNT ON
INSERT INTO #rezults
SELECT @xg3,@xg1,@xg2
SELECT @mess=CAST(@xg1 AS VARCHAR(40))+' /COBJ: '+CAST(@xg3 AS VARCHAR(40))+' /TIME OFF: '+CAST(@NEWTIME AS VARCHAR(40))+' /TIME REACT:' + CAST(@logr AS VARCHAR(40))
PRINT @mess
SET @NEWTIME= NULL
END
SET @xg2=@xg2+1
END
End
FETCH NEXT FROM react_kursor
INTO @xg1,@xg3
END
CLOSE react_kursor
DEALLOCATE react_kursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment