查找担保圈-step5-比较各组之间的成员,对组的包含性进行查询,具体见程序的注释-版本2

 1 USE [test]
 2 GO
 3 /****** Object:  StoredProcedure [dbo].[p03_get_groupno_e2]    Script Date: 2019/7/8 15:01:22 ******/
 4 SET ANSI_NULLS ON
 5 GO
 6 SET QUOTED_IDENTIFIER ON
 7 GO
 8 ALTER PROC [dbo].[p03_get_groupno_e2]
 9 AS
10 BEGIN
11     --s3,比较各组的成员,对组进行清理,具体见程序的注释
12     SET STATISTICS TIME OFF;
13     SET STATISTICS IO OFF;
14     SET NOCOUNT ON;
15     DECLARE @get_max_groupno INT = 0;
16     SELECT @get_max_groupno = MAX(groupno)
17     FROM dbo.t01_get_group_path;
18     --预先清空dbo.t03_get_groupno表
19     TRUNCATE TABLE dbo.t03_get_groupno;
20     DECLARE @a INT = 1;
21     WHILE @a <= @get_max_groupno --最大的分组编号
22     BEGIN
23         DECLARE @b INT = @a + 1;
24         WHILE @b <= @get_max_groupno --最大的分组编号
25         BEGIN
26             DECLARE @p BIT = 0,
27                     @q BIT = 0;
28             IF EXISTS
29             (
30                 SELECT cust_name
31                 FROM [dbo].[t02_get_group_member]
32                 WHERE groupno = @a
33                 EXCEPT
34                 SELECT cust_name
35                 FROM [dbo].[t02_get_group_member]
36                 WHERE groupno = @b
37             )
38                 SET @p = 1;
39             ELSE
40                 SET @p = 0;
41 
42             IF EXISTS
43             (
44                 SELECT cust_name
45                 FROM [dbo].[t02_get_group_member]
46                 WHERE groupno = @b
47                 EXCEPT
48                 SELECT cust_name
49                 FROM [dbo].[t02_get_group_member]
50                 WHERE groupno = @a
51             )
52                 SET @q = 1;
53             ELSE
54                 SET @p = 0;
55             IF (
56                    @p >= 1
57                    OR @p = 0
58                )
59                AND @q = 0 --第1,4种情况,集合a包括集合b的情况(a包括b但b不包括a,a=b两种情况,保留集合a的组号)
60                 INSERT INTO dbo.t03_get_groupno
61                 (
62                     ctype,
63                     iinclude_groupno,
64                     ibeincluded_groupno
65                 )
66                 VALUES
67                 ('1,4', @a, @b);
68             ELSE IF @p = 0
69                     AND @q >= 1 --第2种情况,集合b包括集合a,但集合a不包括集合b
70                 INSERT INTO dbo.t03_get_groupno
71                 (
72                     ctype,
73                     iinclude_groupno,
74                     ibeincluded_groupno
75                 )
76                 VALUES
77                 ('2', @b, @a);
78 
79             ELSE IF @p >= 1 --第三种情况,a和b存在交集但不完全相同,或者a和b完全不同
80                     AND @q >= 1
81             BEGIN
82                 INSERT INTO dbo.t03_get_groupno
83                 (
84                     ctype,
85                     iinclude_groupno,
86                     ibeincluded_groupno
87                 )
88                 VALUES
89                 ('3', @a, 0),
90                 ('3', @b, 0);
91             END;
92             SET @b = @b + 1;
93         END;
94         SET @a = @a + 1;
95     END;
96 END;

 

posted on 2019-07-08 15:02  hold_on_up  阅读(179)  评论(0编辑  收藏  举报