我知道一个字段的内容A通过这个字段A的内容去寻找一个目录树的sql-server的一张表。在表里面寻找A的子项,
我现在想枚举A子项里面的记录,有子节点的循环到里面,找出最后一级的内容添加到另外一张表。没有子节点的直接加入另外一张表。 procedure TchukuBaseFrame.add(id:string;mount:real);
var idstr:string;
begin
  opensql(ado,'select id,artno,mount,sortname,parentid from sort where parentid='+quotedstr(id)+' and sortname  is not null');
  if not ado.IsEmpty then
  begin
    ado.First;
    while not ado.Eof do
    begin
      idstr:=ado.fieldbyname('id').AsString;
      opensql(ado1,'select id,artno,mount,parentid from sort where parentid='+quotedstr(idstr)+' and sortname is not null');
      if ado1.IsEmpty then
      begin
        udb.SaveData('chukumx','id,Pitem,mount,memo',' '+quotedstr(master.fieldbyname('id').AsString)+','+quotedstr(ado.fieldbyname('artno').AsString)+','+quotedstr(floattostr(ado.fieldbyname('mount').AsFloat*mount))+','+quotedstr(lltemp.fieldbyname('yt').AsString));
      end
      else begin
        id:=ado.fieldbyname('id').AsString;
        mount:=mount*ado1.fieldbyname('mount').AsFloat;
        add(id,mount);
      end;
      ado.Next;
    end;
  end;
end; 
   
  我知道我这样写,有问题,请大侠帮帮我!怎么写,不够分在加。。!

解决方案 »

  1.   

    代码手写,请自行测试修改
    function TForm1.findLeaf(parentid:integer):integer;
    begin
      with ADOQuery1 do begin
        open;
        if Locate( 'parentid',parentid,[]) then begin  {找}
          findLeaf(FieldByName('id').AsInteger);       {用找到记录的id当父id继续找它的孩子}
        end
        else
          result := ADOquery1.RecNo;                   {起到没有再找到时,返回该行的记录号}
      end;
    end;
      

  2.   

    看你的题目,应该用递归的.
    对节点是是否是子节点用(HasChildren==TRUE)函数来判断.你要写来二个函数,先建立主体树,然后,叶子就然另一个函数去递归.全部完成树结构,然后全树再进行一边循环,把没有子节点的放B表,有子结点的放A表。
      

  3.   

    你的ADO数据集是全局的吧,改成局部的。