T-SQL存储过程:类别链提取
提取类别链
表结构:
PCID,CategoryName,ParentID
1 衬衫 0
2 裤子 0
3 长袖衬衫 1
1set ANSI_NULLS ON
2set QUOTED_IDENTIFIER ON
3go
4
5/* 创建:***
6* 时间:200*/0*/16
7* 用途:获取从根结点到叶结点的类别链条
8*/
9CREATE PROCEDURE [dbo].[GetCategorys]
10AS
11DECLARE
12@CACHE_PCID int, --缓存PCID
13@OUT_STR nvarchar(500), --输出的类别链条
14@CACHE_PARENTID int, --缓存的父类别ID
15@CACHE_CATENAME nvarchar(20) --缓存的类别名字
16BEGIN
17--获取所有独立叶结点类别
18---------------------------------------------------------------------------
19DECLARE ENDPOINT_CURSOR CURSOR FOR
20select C.PCID from
21(
22 select distinct PCID from w_ProductCategorys
23) C
24left outer join
25(
26--#PID带子类别的类别
27select distinct B.ParentID from
28(
29 select PCID,ParentID from w_ProductCategorys
30) A
31right outer join
32(
33 select PCID,ParentID from w_ProductCategorys
34) B
35on A.PCID=B.ParentID where A.PCID is not null
36--#PID
37) D
38on C.PCID=D.ParentID where D.ParentID is null
39---------------------------------------------------------------------------
40--临时表
41SELECT PCID,ParentID,CategoryName INTO #Temp_TAB FROM w_ProductCategorys
42--打开游标
43OPEN ENDPOINT_CURSOR;
44
45FETCH NEXT FROM ENDPOINT_CURSOR INTO @CACHE_PCID;
46--游标循环
47WHILE @@FETCH_STATUS=0
48BEGIN
49 --循环分析PCID、ParentID,获取类别链条
50 --获取父类别
51 SET @CACHE_PARENTID=(SELECT ParentID FROM #Temp_TAB);
52 --获取叶结点类别名
53 SET @OUT_STR=(SELECT CategoryName FROM #Temp_TAB WHERE PCID=@CACHE_PCID);
54 --开始循环
55 WHILE @CACHE_PARENTID!=0
56 BEGIN
57 SET @CACHE_CATENAME=(SELECT CategoryName From #Temp_TAB WHERE PCID=@CACHE_PARENTID);
58 SET @OUT_STR=@CACHE_CATENAME+'----'+@OUT_STR;
59 SET @CACHE_PARENTID=(SELECT ParentID FROM #Temp_TAB WHERE PCID=@CACHE_PARENTID);
60 END
61
62 PRINT(@OUT_STR);
63 SET @OUT_STR='';
64
65 FETCH NEXT FROM ENDPOINT_CURSOR INTO @CACHE_PCID;
66END
67--关闭游标
68CLOSE ENDPOINT_CURSOR;
69DEALLOCATE ENDPOINT_CURSOR
2set QUOTED_IDENTIFIER ON
3go
4
5/* 创建:***
6* 时间:200*/0*/16
7* 用途:获取从根结点到叶结点的类别链条
8*/
9CREATE PROCEDURE [dbo].[GetCategorys]
10AS
11DECLARE
12@CACHE_PCID int, --缓存PCID
13@OUT_STR nvarchar(500), --输出的类别链条
14@CACHE_PARENTID int, --缓存的父类别ID
15@CACHE_CATENAME nvarchar(20) --缓存的类别名字
16BEGIN
17--获取所有独立叶结点类别
18---------------------------------------------------------------------------
19DECLARE ENDPOINT_CURSOR CURSOR FOR
20select C.PCID from
21(
22 select distinct PCID from w_ProductCategorys
23) C
24left outer join
25(
26--#PID带子类别的类别
27select distinct B.ParentID from
28(
29 select PCID,ParentID from w_ProductCategorys
30) A
31right outer join
32(
33 select PCID,ParentID from w_ProductCategorys
34) B
35on A.PCID=B.ParentID where A.PCID is not null
36--#PID
37) D
38on C.PCID=D.ParentID where D.ParentID is null
39---------------------------------------------------------------------------
40--临时表
41SELECT PCID,ParentID,CategoryName INTO #Temp_TAB FROM w_ProductCategorys
42--打开游标
43OPEN ENDPOINT_CURSOR;
44
45FETCH NEXT FROM ENDPOINT_CURSOR INTO @CACHE_PCID;
46--游标循环
47WHILE @@FETCH_STATUS=0
48BEGIN
49 --循环分析PCID、ParentID,获取类别链条
50 --获取父类别
51 SET @CACHE_PARENTID=(SELECT ParentID FROM #Temp_TAB);
52 --获取叶结点类别名
53 SET @OUT_STR=(SELECT CategoryName FROM #Temp_TAB WHERE PCID=@CACHE_PCID);
54 --开始循环
55 WHILE @CACHE_PARENTID!=0
56 BEGIN
57 SET @CACHE_CATENAME=(SELECT CategoryName From #Temp_TAB WHERE PCID=@CACHE_PARENTID);
58 SET @OUT_STR=@CACHE_CATENAME+'----'+@OUT_STR;
59 SET @CACHE_PARENTID=(SELECT ParentID FROM #Temp_TAB WHERE PCID=@CACHE_PARENTID);
60 END
61
62 PRINT(@OUT_STR);
63 SET @OUT_STR='';
64
65 FETCH NEXT FROM ENDPOINT_CURSOR INTO @CACHE_PCID;
66END
67--关闭游标
68CLOSE ENDPOINT_CURSOR;
69DEALLOCATE ENDPOINT_CURSOR
70END