数据库结构
ID  地区   上级
1   东北   1
2   辽宁   1
3   吉林   1
4   黑龙江 1
5   沈阳   2
6   抚顺   2
7   西北   7
........
如果我输入ID是1 ,我要得到的结果是它的所有下级(2,3,4,5,6),请给我一个递归算法

解决方案 »

  1.   

    不太好实现,
    不过以前做过是这样处理的
    东北01
    辽宁0101
    沈阳010101
    在查询时用like ‘01’就可以实现
    供参考
      

  2.   

    select from where 上级>id
      

  3.   

    ID  地区   上级
    procedure sp_AAA  @变量 ,@outputRrr

    declare c1 cursor for select id ,上级 from table1 where 上级=@变量
    declare @vvvv
    open c1
    fetch c1  @id
    while @@fetch_status=0
    begin
        exec @id,@vvvv
        @outputRrr=,@outputRrr+@vvvv
        fetch c1  @id   
    end
      

  4.   

    我这种写法为什么不行
    Function  TFmUserReporter.GetAlldepart(deptid:integer):String;
    Begin
      ssql:='select dept_id from Department where Upper_dept='''+Inttostr(deptid)+'''';
      Fmdatamodule.qgetalldepart.SQL.Text:=ssql;
      FMdatamodule.qgetalldepart.Open;  While NOt  FMdatamodule.qgetalldepart.Eof Do
      Begin
          Getdepartid:=Getdepartid+Fmdatamodule.qgetalldepart.Fields[0].AsString+',';
          GetAlldepart(Fmdatamodule.qgetalldepart.Fields[0].asinteger);
          Fmdatamodule.qgetalldepart.Next;
      End;
    End;
      

  5.   

    你的上级字段是什么类型的?
    还有像Fmdatamodule.qgetalldepart.SQL.Text:=ssql;
    这种的最好用SQL.Add方法
      

  6.   

    Function  TFmUserReporter.GetAlldepart(deptid:integer):String;
    Begin
      ssql:='select dept_id from Department where Upper_dept='''+Inttostr(deptid)+'''';
      Fmdatamodule.qgetalldepart.SQL.Text:=ssql;
      FMdatamodule.qgetalldepart.Open;  While NOt  FMdatamodule.qgetalldepart.Eof Do
      Begin
        //这个地方用RESULT
        Result := Fmdatamodule.qgetalldepart.Fields[0].AsString +',' + GetAlldepart(Fmdatamodule.qgetalldepart.Fields[0].asinteger);
        Fmdatamodule.qgetalldepart.Next;
      End;
    End;
      

  7.   

    Function  TFmUserReporter.GetAlldepart(deptid:integer):String;
    Begin
      Result := '';
      ssql:='select dept_id from Department where Upper_dept='''+Inttostr(deptid)+'''';
      Fmdatamodule.qgetalldepart.SQL.Text:=ssql;
      FMdatamodule.qgetalldepart.Open;  While NOt  FMdatamodule.qgetalldepart.Eof Do
      Begin
        //这个地方用RESULT
        Result := Result + Fmdatamodule.qgetalldepart.Fields[0].AsString +',' + GetAlldepart(Fmdatamodule.qgetalldepart.Fields[0].asinteger);
        Fmdatamodule.qgetalldepart.Next;
      End;
    End;
      

  8.   

    我介绍一种不一样的方法(没测试过代码,看懂意思就行)
    -------------------------------
    prcedure GetChildren(AResultDataSet: TADODataSet; AParentID: string);
    var
      adsTemp: TADODataSet;
    begin
      cmdTemp: = '';
      adsTemp:= TADODataSet.Create(nil); 
      try
        adsTemp.CommandText := 'SELECT * FROM yourtable where 上级='+QuotedStr(AParentID);
        adsTemp.Open;
        while not adsTemp.isEmpty then
        begin
          adsTemp.CommandText := adsTemp.Commandtext+' OR 上级 in ('+cmdText+')';
        end;
        if ADataSet.Active then ADataSet.Close;
        ADataSet.CommandText := adsTemp.CommandText;
        ADataSet.Open;
      finally
        adsTemp.Free;
      end;    
    end;
      

  9.   

    修改一下:
    ----------------------
    -------------------------------
    prcedure GetChildren(AResultDataSet: TADODataSet; AParentID: string);
    var
    adsTemp: TADODataSet;
    recCount: integer;
    begin
    cmdTemp: = '';
    adsTemp:= TADODataSet.Create(nil);
    try
    adsTemp.CommandText := 'SELECT * FROM yourtable where 上级='+QuotedStr(AParentID);
    adsTemp.Open;
    recCount:= adsTemp.RecordCount;
    while recCount>0 do
    begin
    adsTemp.CommandText := adsTemp.Commandtext+' OR 上级 in ('+cmdText+')';
    adsTemp.Open;
    if recCount <> adsTemp.recordCount then
      recCount = adsTemp.recordCount
    else 
      break;
    end;
    if ADataSet.Active then ADataSet.Close;
    ADataSet.CommandText := adsTemp.CommandText;
    ADataSet.Open;
    finally
    adsTemp.Free;
    end;
    end;
      

  10.   

    方法如上
    修改了一点
    procedure TForm1.Button1Click(Sender: TObject);
    var
    Temsql:string;
    Reccount:integer;
    Result :string;
    begin
         with adoquery1 do
         begin
              close;
              sql.Clear;          sql.Add(format('select id  from Table2 where pid = %d and id <> %d',[strtoint(trim(edit1.text)),strtoint(trim(edit1.text))]));
              open;
              Reccount := adoquery1.RecordCount;          while Reccount > 0 do
              begin
                   with  adoquery2 do
                   begin
                         close;
                         sql.Clear;
                         sql.add( adoquery1.SQL.Text+'or pid in ('+ adoquery1.SQL.Text+')');
                         Temsql := adoquery2.SQL.Text;
                         open;
                         if  adoquery2.RecordCount >  Reccount then
                              Reccount := adoquery2.RecordCount
                         else
                              break;
                   end;          end;
         end;     with  adoquery2 do
         begin
              first;
              Result := '';
              while not eof do
              begin
                    Result :=  Result +   inttostr(FieldBYname('id').asinteger)+',';
                    next;
              end;          edit1.Text := Result;
         end;
    end;