日期:2014-05-17  浏览次数:20352 次

多级目录复制问题
多级目录复制问题,项目A包含项目B,C,项目B,C下面还有包含项目D,F...。设表结构为id,name,p_id(父结点号)。当以A为模板进行复制时,复制的记录如何包含A下面的所有项目。 请给一些好的建议,包括表结构。
多级,复制,表结构

------解决方案--------------------
简单写了个例子,你再看不懂,我也帮不上你了。
表格t中,含有2个项目,数据如下:
id          p_id        Name
----------- ----------- ----------
1           NULL        1
2           1           2
3           2           3
4           1           4
5           NULL        5
6           5           6
7           6           7
8           7           8

步骤1,2(放一起了): select * into tt from t
3,4. 
declare @num int = (select count(*) from t
where t.p_id in (select id from tt)
and id not in (select id from tt))
select @num

while @num <> 0
begin
set @num = (select count(*) from t
where t.p_id in (select id from tt)
and id not in (select id from tt)
)
select @num

insert into tt
select * from t
where t.p_id in (select id from tt)
and id not in (select id from tt)
end

你在while循环的insert段落写成:
insert into tt
select id*100, name, p_id*100 from t
where t.p_id in (select id from tt)
and id not in (select id from tt)
也可以,但是注意别写入tt表中已存在的id,p_id就行