日期:2014-05-18 浏览次数:20522 次
--DELETE FROM JIEGUO DECLARE @ID VARCHAR DECLARE Cur_Cour SCROLL CURSOR FOR SELECT DISTINCT dbo.UDT_M_183.UDF_M_1539 FROM dbo.UDT_M_183 INNER JOIN dbo.UDT_S_182 ON dbo.UDT_M_183.RID = dbo.UDT_S_182.RID WHERE dbo.UDT_S_182.UDF_F_1069 = 'SCJH120206001' ORDER BY dbo.UDT_M_183.UDF_M_1539 DESC open Cur_Cour FETCH NEXT FROM Cur_Cour INTO @ID WHILE @@FETCH_STATUS=0 BEGIN --------------------- WITH Relation(ID, RCName, ParentID,LVL,Name_List) AS ( SELECT D.ID, D.RCName , ParentID,1 LVL,CAST(RCName AS VARCHAR(255)) Name_List FROM dbo.UDT_M_655 D -- WHERE PARENTID = '4P172183-1C' WHERE PARENTID = @ID UNION ALL SELECT T.ID, T.RCName, T.ParentID ,LVL+1 LVL , CAST((Name_List + '/' + T.RCName) AS VARCHAR(255)) Name_List FROM dbo.UDT_M_655 T, Relation P WHERE 1=1 AND P.ID = T.ParentID ) INSERT INTO JIEGUO(ID, RCName, ParentID,LVL,Name_List) SELECT ID, RCName, ParentID,LVL,Name_List FROM Relation ------------------------------------------ FETCH NEXT FROM Cur_Cour END Close Cur_Cour DEALLOCATE Cur_Cour SELECT *FROM JieGuo
WITH Relation(ID, RCName, ParentID,LVL,Name_List) AS ( SELECT D.ID, D.RCName , ParentID,1 LVL,CAST(RCName AS VARCHAR(255)) Name_List FROM dbo.UDT_M_655 D -- WHERE PARENTID = '4P172183-1C' WHERE PARENTID IN( SELECT DISTINCT dbo.UDT_M_183.UDF_M_1539 FROM dbo.UDT_M_183 INNER JOIN dbo.UDT_S_182 ON dbo.UDT_M_183.RID = dbo.UDT_S_182.RID WHERE dbo.UDT_S_182.UDF_F_1069 = 'SCJH120206001') UNION ALL SELECT T.ID, T.RCName, T.ParentID ,LVL+1 LVL , CAST((Name_List + '/' + T.RCName) AS VARCHAR(255)) Name_List FROM dbo.UDT_M_655 T, Relation P WHERE 1=1 AND P.ID = T.ParentID ) SELECT * FROM relation
------解决方案--------------------
还是建议来一个唯一标识列,这样比较好处理点
------解决方案--------------------
WITH Relation(ID, RCName, ParentID,LVL,Name_List) AS ( SELECT D.ID, D.RCName , ParentID,1 LVL,CAST(RCName AS VARCHAR(255)) Name_List FROM dbo.UDT_M_655 D -- WHERE PARENTID = '4P172183-1C' WHERE PARENTID IN( SELECT dbo.UDT_M_183.UDF_M_1539 --去掉这处该死的distinct就行。 FROM dbo.UDT_M_183 INNER JOIN dbo.UDT_S_182 ON dbo.UDT_M_183.RID = dbo.UDT_S_182.RID WHERE dbo.UDT_S_182.UDF_F_1069 = 'SCJH120206001') UNION ALL SELECT T.ID, T.RCName, T.ParentID ,LVL+1 LVL , CAST((Name_List + '/' + T.RCName) AS VARCHAR(255)) Name_List FROM dbo.UDT_M_655 T, Relation P WHERE 1=1 AND P.ID = T.ParentID ) SELECT * FROM relation
------解决方案--------------------
因为你的游标后面FETCH NEXT FROM Cur_Cour
错了
少了 into @id