Skip to content

Commit da02998

Browse files
Create p_RecursiveMembershipSnapshot.sql
1 parent f7d1142 commit da02998

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
--Create the cleanup sproc:
2+
USE [OperationsManager]
3+
GO
4+
/****** Object: StoredProcedure [dbo].[p_RecursiveMembershipSnapshot] Script Date: 8/30/2017 6:38:45 PM ******/
5+
SET ANSI_NULLS ON
6+
GO
7+
SET QUOTED_IDENTIFIER ON
8+
GO
9+
CREATE PROCEDURE [dbo].[p_RecursiveMembershipSnapshot]
10+
AS
11+
BEGIN
12+
DECLARE @LastErr int;
13+
DECLARE @RowCount int;
14+
DECLARE @Depth int;
15+
DECLARE @RelationshipTypeId uniqueidentifier = dbo.fn_ManagedTypeId_SystemContainment();
16+
CREATE TABLE #RecursiveMembershipTemp (
17+
[ContainerEntityId] uniqueidentifier NOT NULL,
18+
[ContainedEntityId] uniqueidentifier NOT NULL,
19+
[Depth] [int] NOT NULL
20+
);
21+
SET @LastErr = @@ERROR;
22+
IF @LastErr <> 0
23+
GOTO Err;
24+
BEGIN TRANSACTION;
25+
-- ddp lock
26+
UPDATE [dbo].[DiscoverySource]
27+
SET [TimeGeneratedOfLastSnapshot] = [TimeGeneratedOfLastSnapshot]
28+
WHERE [DiscoverySourceId] = '85AB926D-6E0F-4B36-A951-77CCD4399681';
29+
SET @LastErr = @@ERROR;
30+
IF @LastErr <> 0
31+
GOTO Err;
32+
SET @Depth = 0;
33+
INSERT INTO #RecursiveMembershipTemp
34+
([ContainerEntityId], [ContainedEntityId], [Depth])
35+
SELECT
36+
[BaseManagedEntityId], [BaseManagedEntityId], @Depth
37+
FROM dbo.[BaseManagedEntity]
38+
WHERE [IsDeleted] = 0;
39+
SELECT @LastErr = @@ERROR, @RowCount = @@ROWCOUNT;
40+
IF @LastErr <> 0
41+
GOTO Err;
42+
WHILE (@RowCount > 0)
43+
BEGIN
44+
INSERT INTO #RecursiveMembershipTemp
45+
([ContainerEntityId], [ContainedEntityId], [Depth])
46+
SELECT
47+
RR.[ContainerEntityId], R.[TargetEntityId], @Depth + 1
48+
-- Filtering by RelationshipTypes that derive from 'Containment'
49+
FROM dbo.[RelationshipType] AS RT
50+
INNER JOIN dbo.fn_DerivedRelationshipTypes(@RelationshipTypeId) DRT
51+
ON DRT.[DerivedRelationshipTypeId] = RT.[RelationshipTypeId]
52+
INNER JOIN dbo.[Relationship] AS R
53+
ON RT.[RelationshipTypeId] = R.[RelationshipTypeId]
54+
AND R.[IsDeleted] = 0
55+
INNER JOIN #RecursiveMembershipTemp AS RR
56+
ON RR.[ContainedEntityId] = R.[SourceEntityId]
57+
AND RR.Depth = @Depth;
58+
SELECT @LastErr = @@ERROR, @RowCount = @@ROWCOUNT
59+
IF @LastErr <> 0
60+
GOTO Err;
61+
SET @Depth = @Depth + 1;
62+
END
63+
-- Populate clean RecursiveMembership.
64+
TRUNCATE TABLE dbo.[RecursiveMembership];
65+
INSERT INTO dbo.[RecursiveMembership]
66+
([ContainerEntityId], [ContainedEntityId], [Depth], [PathCount])
67+
SELECT
68+
[ContainerEntityId], [ContainedEntityId], MIN([Depth]), COUNT(1)
69+
FROM #RecursiveMembershipTemp
70+
GROUP BY [ContainerEntityId], [ContainedEntityId];
71+
72+
SET @LastErr = @@ERROR;
73+
IF @LastErr <> 0
74+
GOTO Err;
75+
COMMIT TRANSACTION;
76+
DROP TABLE #RecursiveMembershipTemp;
77+
RETURN 0;
78+
Err:
79+
ROLLBACK TRANSACTION;
80+
RETURN 1;
81+
END
82+
GO
83+

0 commit comments

Comments
 (0)