简体   繁体   English

递归SQL Server查询4

[英]Recursive sql server query 4

I have 2 tables t1 and t2 with columns objectid, parentid and objectname. 我有2个表t1和t2,其列为objectid,parentid和objectname。 for each t1.objectid id I need to navigate t2 until t2.parentid is null and concatenate t2.objectname. 对于每个t1.objectid id,我需要导航t2直到t2.parentid为null并连接t2.objectname。 The navigation criteria is that t1.objectid=t2.parentid .. traverse until t2.parent id is null. 导航标准是遍历t1.objectid = t2.parentid ..直到t2.parent id为null。 I tried cursor however I am not getting desired result. 我尝试过光标,但是没有得到想要的结果。

SCHEMA 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. 看起来您的样本数据无效,因为在t2中,对象名称F4和F7引用相同的对象ID。

Other than that, te following recursive cte should return the expected result: 除此之外,以下递归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 is null
ORDER BY grp -- can be omitted once the sample data are logically correct

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM