且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

递归sql server查询4

更新时间:2023-02-05 23:37:55

看起来您的示例数据无效因为在t2中,对象名称F4和F7引用相同的objectid。



除此之外,te下面的递归cte应该返回预期结果:



WITH MyCTE AS

SELECT objectid as grp,parentid,CAST(objectname as varchar(201))as objectname2
FROM t1
UNION ALL
SELECT Mycte.grp,t2.parentid,CAST(t2.objectname as varchar(100))+'/'+ CAST(mycte.objectname2 as varchar(100))
FROM t2
INNER JOIN MyCTE ON MyCTE.parentid = t2.objectid

SELECT grp,objectname2
FROM MyCTE
WHERE parentid为null
ORDER BY grp - 一旦样本数据在逻辑上正确就可以省略


I have 2 tables t1 and t2 with columns objectid, parentid and objectname. for each t1.objectid id I need to navigate t2 until t2.parentid is null and concatenate t2.objectname. The navigation criteria is that t1.objectid=t2.parentid .. traverse until t2.parent id is null. I tried cursor however I am not getting desired result.

SCHEMA

create table t1(objectid varchar(100), parentid varchar(100),objectname varchar(100))
go
create table t2(objectid varchar(100), parentid varchar(100),objectname varchar(100))
go
insert into t1 
select '265-0151CDDF-F032-4E47-98B2-236127258C81',NULL,'F1'
union 
select '265-091D3023-4B6A-4822-8409-AC0807DA9EB4','265-CC340F32-E97D-45CE-8019-26CE0FF99663','F2'
union 
select '265-2684E597-5A0A-4656-B1D5-FED27F67ADC9','265-EEA1BB1D-E4F3-43C4-9D3B-625CE4AB8FEE','F3'

GO
insert into t2
select '265-0151CDDF-F032-4E47-98B2-236127258C81',NULL,'F1'
union 
select '265-091D3023-4B6A-4822-8409-AC0807DA9EB4','265-CC340F32-E97D-45CE-8019-26CE0FF99663','F2'
union 
select '265-CC340F32-E97D-45CE-8019-26CE0FF99663',NULL,'F4'
union
select '265-2684E597-5A0A-4656-B1D5-FED27F67ADC9','265-EEA1BB1D-E4F3-43C4-9D3B-625CE4AB8FEE','F5'
union
select '265-EEA1BB1D-E4F3-43C4-9D3B-625CE4AB8FEE','265-CC340F32-E97D-45CE-8019-26CE0FF99663','F6'
union
select '265-CC340F32-E97D-45CE-8019-26CE0FF99663',NULL,'F7'

Desired output:

objectid                                 parentid    objectname
---------------------------------------- ----------- ----------
265-0151CDDF-F032-4E47-98B2-236127258C81 NULL        F1
265-091D3023-4B6A-4822-8409-AC0807DA9EB4 NULL        F4/F2
265-2684E597-5A0A-4656-B1D5-FED27F67ADC9 NULL        F7/F6/F3

Solution (not working)

declare @objectid nvarchar(max)
declare @parentid nvarchar(max)
declare @foldername nvarchar(100)
Declare @tbl TABLE (objectid nvarchar(max), parentid nvarchar(max) ,fdn nvarchar(max))
declare @innerfoldername nvarchar(max)
declare @fdn nvarchar(max)

declare outer_cursor cursor  for 
Select  
    objectid,
    parentid,
    objectname 
 from t1

open  outer_cursor 
fetch outer_cursor into @objectid, @parentid,@foldername

while(@@FETCH_STATUS=0)
BEGIN
    /*
    insert into @tbl
    SELECT @objectid, @parentid,@foldername
    */
    declare inner_cursor cursor for 
    select parentid,objectname from t2 where objectid=@parentid

    open inner_cursor 
    fetch inner_cursor into @parentid,@innerfoldername
    set @fdn=@foldername
    while(@@FETCH_STATUS=0)
    BEGIN
        set @fdn=@innerfoldername + '/' + @fdn 
    --  select @objectid,@parentid,@fdn
        insert into @tbl
        SELECT @objectid,@parentid,@fdn 

        fetch inner_cursor into @parentid,@innerfoldername

    END
    close inner_cursor
    deallocate inner_cursor
    fetch outer_cursor into @objectid, @parentid,@foldername
END
close outer_cursor 
deallocate outer_cursor 
select * from @tbl

It looks like your sample data are invalid since in t2 the objectnames F4 and F7 refer to the same objectid.

Other than that, te following recursive cte should return the expected result:

WITH MyCTE AS 
(
    SELECT  objectid as grp, parentid, CAST(objectname as varchar(201)) as objectname2
    FROM t1
    UNION ALL
    SELECT Mycte.grp, t2.parentid, CAST(t2.objectname as varchar(100)) +'/'+ CAST(mycte.objectname2 as varchar(100))
    FROM t2
    INNER JOIN MyCTE ON MyCTE.parentid = t2.objectid
)
SELECT grp, objectname2
FROM MyCTE
WHERE parentid is null
ORDER BY grp -- can be omitted once the sample data are logically correct