各位大俠你們好
小弟這裡遇到個為題是這樣的:
A為主表
B為A得從表 通過IND_NO聯系  C為B的從表 通過BATCH_NO聯系
為了需要先對A進行循環 得到IND_NO得條件串 去B表找出符合條件的所有紀錄A循環 {
  B循環{
     找所有B中的BATCH_NO字段值組成條件串 為了出所有C表中的資料
      }
}
找c表所有資料的SQL
當再對A循環是
A{
 IF B.RECORDCOUNT THEN  //(B.RECORDCOUNT得紀錄數出現問題)
 {  }}
具體代碼入下請大俠指點:
cdsMaster.first;
while not cdsMaster.Eof do
begin
 
  ..........  clMsql:=clMsql+' or(ind_no='+#39+trim(qryMaster.fieldByName('IND_NO').AsString)+#39+')';
  cdsMaster.Next;
end;//找從表B資料
pkSql:='selectUPDATE_USER,UPDATE_DATE_TIME,IND_NO,NUM_FIRST,NUM_LAST,PACK_MODE,
ITEM_NO,PER_VOLUME,NUM_LAST-NUM_FIRST+1 as PKG_NUM,BATCH_NO '+' from ord_pkg_mast where '+copy(clMSql,4,length(clMSql)-2)+' order by IND_NO,BATCH_NO';
    cdsPkg_Detl.Close;
    cdsPkg_Detl.CommandText:=pkSql; 
    cdsPkg_Detl.Open;//利用從表B 找出流水號BATCH_NO
    cdsMaster.First;
    while not cdsMaster.Eof do
    begin
      if cdsPkg_Detl.RecordCount<>0 then
      begin
        cdsPkg_Detl.First;
        while not cdsPkg_Detl.Eof do
        begin
          clSql:=clSql+' or(BATCH_NO='+#39+cdsPkg_Detl.FieldByName('BATCH_NO').AsString+#39+')';
          cdsPkg_Detl.Next;
        end;
      end;
      cdsMaster.Next;
    end;
 根據從表B找出的流水號查找從從表c的所有資料
      if clSql<>'' then
      begin
        spDetlSql:='select * from ord_pkg_detl where '+
                   copy(clSql,4,length(clSql)-2)+' order by BATCH_NO';
        codeSite.SendMsg(spDetlSql);
        cdsSize_detl.CommandText:=spDetlSql;
        cdsSize_detl.Open;
      end;    cdsMaster.first;//再次對A表循環
    while not cdsMaster.Eof do
    begin
      //沒有資料則生成默認裝箱流程
      if cdsPkg_detl.RecordCount=0 then // 從表 cdsPkg_detl.RecordCount 出現問題 
當我測試時有三筆主表的IND_NO :ASD70001 ASD70002  ASD70003  其中前兩筆在B表(ord_pkg_mast)中可以找到資料
當條件只用這兩筆側時 主表在第一筆ASD70001時 cdsPkg_detl.RecordCount=7正常  當主表在第二筆ASD70002時cdsPkg_detl.RecordCount=15正常
當條件范圍為ASD70001--ASD70003時 主表在第一筆ASD70001時 cdsPkg_detl.RecordCount=21筆出現錯誤正常應該也是7筆
當主表在第二筆ASD70002時 cdsPkg_detl.RecordCount=60 正常是15 
請大俠指點

      begin
        ..........
      end;

解决方案 »

  1.   

    具體代碼入下請大俠指點: 
    cdsMaster.first; 
    while not cdsMaster.Eof do 
    begin 
      
      ..........   clMsql:=clMsql+ ' or(ind_no= '+#39+trim(qryMaster.fieldByName( 'IND_NO ').AsString)+#39+ ') '; 
      cdsMaster.Next; 
    end; //找從表B資料 
    pkSql:= 'selectUPDATE_USER,UPDATE_DATE_TIME,IND_NO,NUM_FIRST,NUM_LAST,PACK_MODE, 
    ITEM_NO,PER_VOLUME,NUM_LAST-NUM_FIRST+1 as PKG_NUM,BATCH_NO  '+ ' from ord_pkg_mast where  '+copy(clMSql,4,length(clMSql)-2)+ ' order by IND_NO,BATCH_NO '; 
        cdsPkg_Detl.Close; 
        cdsPkg_Detl.CommandText:=pkSql;  
        cdsPkg_Detl.Open; //利用從表B 找出流水號BATCH_NO 
        cdsMaster.First; 
        while not cdsMaster.Eof do 
        begin 
          if cdsPkg_Detl.RecordCount < >0 then (這裡主表循環可以正確紀錄從表此時的紀錄數;的顯示為什麼後面再循環確出現問題)
          begin 
            cdsPkg_Detl.First; 
            while not cdsPkg_Detl.Eof do 
            begin 
              clSql:=clSql+ ' or(BATCH_NO= '+#39+cdsPkg_Detl.FieldByName( 'BATCH_NO ').AsString+#39+ ') '; 
              cdsPkg_Detl.Next; 
            end; 
          end; 
          cdsMaster.Next; 
        end; 
     根據從表B找出的流水號查找從從表c的所有資料 
          if clSql < > ' ' then 
          begin 
            spDetlSql:= 'select * from ord_pkg_detl where  '+ 
                       copy(clSql,4,length(clSql)-2)+ ' order by BATCH_NO '; 
            codeSite.SendMsg(spDetlSql); 
            cdsSize_detl.CommandText:=spDetlSql; 
            cdsSize_detl.Open; 
          end;     cdsMaster.first;//再次對A表循環 
        while not cdsMaster.Eof do 
        begin 
          //沒有資料則生成默認裝箱流程 
          if cdsPkg_detl.RecordCount=0 then // 從表 cdsPkg_detl.RecordCount 出現問題  
    當我測試時有三筆主表的IND_NO :ASD70001 ASD70002  ASD70003  其中前兩筆在B表(ord_pkg_mast)中可以找到資料 
    當條件只用這兩筆側時 主表在第一筆ASD70001時 cdsPkg_detl.RecordCount=7正常  當主表在第二筆ASD70002時cdsPkg_detl.RecordCount=15正常 
    當條件范圍為ASD70001--ASD70003時 主表在第一筆ASD70001時 cdsPkg_detl.RecordCount=21筆出現錯誤正常應該也是7筆 
    當主表在第二筆ASD70002時 cdsPkg_detl.RecordCount=60 正常是15  
    請大俠指點       begin 
            .......... 
          end; 
      

  2.   

    使用视图返回主从表数据,然后对结果集进行filter过滤