利用 TstringList 循环取数据 应用例子
procedure GetData; var BillNoList,ActidList:TStrings; i:integer; begin ActidList:=TStringList.Create(); BillNoList:=TStringList.Create(); with SQLQuerySrcEntityid do begin Open;First; while not Eof do begin ActidList.Add(FieldByName('SrcActid').asString); BillNoList.add(FieldByName('SrcBsbillno').asString); Next; end; end; with SQLQueryDss do begin Close;SQL.Clear; SQLConnection:=SQLConn; SQL.Add('Select distinct i.Sysid,i.SrcActid,i.SrcEntityid,a.Corpid,a.Billno,i.Srcbsbillno '); SQL.Add(',i.Mblno,i.Eta,i.Etd,i.Pod,i.Vsl,i.Voy,i.ContainerQty,c.Sname CorpName') SQL.Add(',a.Amt,(a.Amt-a.StlAmt) AmtBalance,a.FcyAmt,(a.FcyAmt-a.StlFcy) FcyBalance'); SQL.Add('from accbillsitems i'); SQL.Add('Left join accbills a on (a.Sysid=i.sysid and a.Entityid=i.Entityid)'); SQL.Add('Left join Corps c on (c.sysid=a.sysid and a.corpid=c.corpid)'); SQL.Add('where i.SysID=:Sysid '); SQL.Add('and SrcActid=:SrcActid'); SQL.Add('and SrcBsbillno=:SrcBsbillNo'); if Fcorpid<>0 then begin SQL.Add('and a.Corpid<>:corpid'); ParamByName('Corpid').AsInteger:=FCorpid; end; SQL.Add('and ((a.Amt-a.Stlamt<>0) or (a.FcyAmt-a.StlFcy<>0))'); SQL.Add('and a.ACcDc=''DR'''); SQL.Add('order by i.Sysid,i.SrcActid,i.Srcbsbillno'); ParamByName('SysID').AsInteger:=FSysID; Prepared:=True; end; try for i:=0 to BillNoList.count-1 do begin SQLQueryDss.close; SQLQueryDss.ParamByName('SrcBsbillNo').AsString:=BillNoList[i]; SQLQueryDss.ParamByName('SrcActid').AsString:=ActidList[i]; SQLQueryDss.Open; SQLQueryDss.First; if not (SQLQueryDss.bof and SQLQueryDss.Eof) then begin while Not SQLQueryDss.Eof do begin with Cdsdss do begin Append; FieldbyName('Sysid').AsInteger:=GetSysid; FieldbyName('SrcActid').AsInteger:=SQLQueryDss.FieldbyName('SrcActid').Asinteger; FieldbyName('SrcEntityid').AsInteger:=SQLQueryDss.FieldbyName('SrcEntityid').Asinteger; FieldbyName('Billno').AsString:=SQLQueryDss.FieldbyName('billNo').AsString; FieldbyName('Srcbsbillno').AsString:=SQLQueryDss.FieldbyName('SrcbsbillNo').AsString; FieldbyName('MblNo').AsString:=SQLQueryDss.FieldbyName('MblNo').AsString; FieldbyName('Pod').AsString:=SQLQueryDss.FieldbyName('pod').AsString; FieldbyName('Vsl').AsString:=SQLQueryDss.FieldbyName('vsl').AsString; FieldbyName('Voy').AsString:=SQLQueryDss.FieldbyName('Voy').AsString; FieldbyName('ContainerQty').AsString:=SQLQueryDss.FieldbyName('ContainerQty').AsString; FieldbyName('CorpName').AsString:=SQLQueryDss.FieldbyName('CorpName').AsString; FieldbyName('Etd').AsString:=FormatDateTime('YYYY-MM-DD',SQLQueryDss.FieldbyName('Etd').AsDateTime); FieldbyName('Amt').Ascurrency:=SQLQueryDss.FieldbyName('Amt').Ascurrency; FieldbyName('AmtBalance').Ascurrency:=SQLQueryDss.FieldbyName('AmtBalance').Ascurrency; FieldbyName('FcyAmt').Ascurrency:=SQLQueryDss.FieldbyName('FcyAmt').Ascurrency; FieldbyName('FcyBalance').Ascurrency:=SQLQueryDss.FieldbyName('FcyBalance').Ascurrency; Post; end; SQLQueryDss.Next; end; end; end; except ShowMessage(ExceptionParam); end; BillNoList.free; ActidList.free; end;