Create Table T
(
id int,
code Varchar(10),
parentid int,
backres Varchar(10)
)
--插入数据
insert into T values(1, 'A', 5,'')
insert into T values(2, 'B', 5,'')
insert into T values(3, 'C', 5,'')
insert into T values(4, 'D', 5,'')
insert into T values(5, 'E', 6,'')
insert into T values(6, 'F', 0,'')
--更新(按parentid把code按分号拼接,然后替换掉本身)
Update T Set
backres=
Replace((Select code+';' From T A Where T.parentid=A.parentid
For Xml Path('')
),code+';','')
--查看结果
Select * from T