我有一张表如下
cat_id Cat_Name Main_Cat_Id
1 veg null
2 main course 1
3 starter 1
4 Indian 2
5 mexican 2
6 tahi 3
7 chinese 3
8 nonveg null
9 main course 8
10 indian 9
11 starter 8
12 tahi 11
13 chinese 11
( Main_Cat_Id 是 cat_id 以前添加的类别)
此表用于产品的类别,其中 veg 类别包含两个子类别主要课程和启动器,其标识为 main_cat_id 那些子类别再次具有印度和墨西哥
的子类别这种分类取决于用户;他可以为印度,墨西哥添加更多子类别,以便他可以进行任何级别的分类
现在我必须选择任何节点的所有子类别,如果我采取veg我必须选择
(1)veg > (2)main course(1) > (4)indian(2)
> (5)mexican(2)
> (3)starter(1) > (6)thai(3)
> (7)chinese(3)
将字符串形成为1,2,4,5,3,6,7
这样做我写了一个sql函数
CREATE FUNCTION [dbo].[GetSubCategory_TEST]
( @MainCategory int, @Category varchar(max))
RETURNS varchar(max)
AS
BEGIN
IF EXISTS (SELECT Cat_Id FROM Category WHERE Main_Cat_Id=@MainCategory)
BEGIN
DECLARE @TEMP TABLE
(
CAT_ID INT
)
INSERT INTO @TEMP(CAT_ID) SELECT Cat_Id FROM Category WHERE Main_Cat_Id=@MainCategory
DECLARE @TEMP_CAT_ID INT
DECLARE CUR_CAT_ID CURSOR FOR SELECT CAT_ID FROM @TEMP
OPEN CUR_CAT_ID
WHILE 1 =1
BEGIN
FETCH NEXT FROM CUR_CAT_ID
INTO @TEMP_CAT_ID;
IF @@FETCH_STATUS <> 0
SET @Category=@Category+','+ CONVERT(VARCHAR(50), @TEMP_CAT_ID)
SET @Category = [dbo].[GetSubCategory](@TEMP_CAT_ID,@Category)
END
CLOSE CUR_CAT_ID
DEALLOCATE CUR_CAT_ID
END
return @Category
END
但是这个功能继续执行而没有提供所需的输出我不明白错误发生了什么plz帮助我得到这个
答案 0 :(得分:8)
你不需要一个递归函数来构建它,你可以使用Recursive CTE。
像
这样的东西DECLARE @TABLE TABLE(
cat_id INT,
Cat_Name VARCHAR(50),
Main_Cat_Id INT
)
INSERT INTO @TABLE SELECT 1,'veg',null
INSERT INTO @TABLE SELECT 2,'main course',1
INSERT INTO @TABLE SELECT 3,'starter',1
INSERT INTO @TABLE SELECT 4,'Indian',2
INSERT INTO @TABLE SELECT 5,'mexican',2
INSERT INTO @TABLE SELECT 6,'tahi',3
INSERT INTO @TABLE SELECT 7,'chinese',3
INSERT INTO @TABLE SELECT 8,'nonveg',null
INSERT INTO @TABLE SELECT 9,'main course',8
INSERT INTO @TABLE SELECT 10,'indian',9
INSERT INTO @TABLE SELECT 11,'starter',8
INSERT INTO @TABLE SELECT 12,'tahi',11
INSERT INTO @TABLE SELECT 13,'chinese',11
;WITH Recursives AS (
SELECT *,
CAST(cat_id AS VARCHAR(MAX)) + '\' ID_Path
FROM @TABLE
WHERE Main_Cat_Id IS NULL
UNION ALL
SELECT t.*,
r.ID_Path + CAST(t.cat_id AS VARCHAR(MAX)) + '\'
FROM @TABLE t INNER JOIN
Recursives r ON t.Main_Cat_Id = r.cat_id
)
SELECT *
FROM Recursives
答案 1 :(得分:0)
我很惭愧,但是我用@astander scipt给出了字符串结果。
首先,我创建了您提供的数据。
其次,我收集了我需要的行
然后使用XML
我将所有内容放在一行中(函数STUFF
删除第一个逗号)
DECLARE @TABLE TABLE(
cat_id INT,
Cat_Name VARCHAR(50),
Main_Cat_Id INT
)
DECLARE @Collected TABLE(
cat_id INT
)
INSERT INTO @TABLE SELECT 1,'veg',null
INSERT INTO @TABLE SELECT 2,'main course',1
INSERT INTO @TABLE SELECT 3,'starter',1
INSERT INTO @TABLE SELECT 4,'Indian',2
INSERT INTO @TABLE SELECT 5,'mexican',2
INSERT INTO @TABLE SELECT 6,'tahi',3
INSERT INTO @TABLE SELECT 7,'chinese',3
INSERT INTO @TABLE SELECT 8,'nonveg',null
INSERT INTO @TABLE SELECT 9,'main course',8
INSERT INTO @TABLE SELECT 10,'indian',9
INSERT INTO @TABLE SELECT 11,'starter',8
INSERT INTO @TABLE SELECT 12,'tahi',11
INSERT INTO @TABLE SELECT 13,'chinese',11
INSERT INTO @TABLE SELECT 14,'chinese',6
DECLARE @nodeID INT = 1;
DECLARE @result VARCHAR(MAX);
;WITH Recursives AS (
SELECT cat_id, main_cat_id
FROM @TABLE
WHERE Cat_Id = @nodeID
UNION ALL
SELECT T.cat_id, T.main_cat_id
FROM @TABLE AS T
INNER JOIN Recursives AS R
ON t.Main_Cat_Id = r.cat_id
)
INSERT INTO @Collected
SELECT cat_id
FROM Recursives
SELECT @result = STUFF(
(SELECT ',' + CAST( cat_id AS VARCHAR)
FROM @Collected
ORDER BY cat_id
FOR XML PATH('')
), 1,1,'')
SELECT @result