sql处理数据困难的办法--用程序处理生成单一sql

procedure TForm1.Button1Click(Sender: TObject);
type
  TSqlItem = record
    BiShu: Integer;
    Amount: Integer;
    TradeStatus: Integer;
  end;
  TSqlItemAry = array of TSqlItem;
var
  LRead, LWrite: TextFile;
  LMonth: Integer;
  LSqlAry: TSqlItemAry;
  LLine: string;
  LStrList: TStringList;
  LJinE, LDianHuaKa, LWangYin: Integer;
  LIndex: Integer;
  I: Integer;
  LTotal: Integer;
begin
  Randomize;

  LStrList := TStringList.Create;
  LStrList.Delimiter := #9;
  LStrList.StrictDelimiter := True;
  LStrList.Duplicates := dupAccept;
  for LMonth := 8 to 12 do
  begin
    AssignFile(LRead, Format('%d.txt', [Integer(LMonth)]));
    AssignFile(LWrite, Format('%d.sql', [Integer(LMonth)]));
    Reset(LRead);
    Rewrite(LWrite);

    SetLength(LSqlAry, 0);
    while not Eof(LRead) do
    begin
      Readln(LRead, LLine);
      LLine := Trim(LLine);
      if Length(LLine) > 0 then
      begin
        LStrList.DelimitedText := LLine;
        if LStrList.Count <> 3 then
          raise Exception.Create('<>3');

        LJinE := StrToIntDef(LStrList[0], 0);
        LDianHuaKa := Round(StrToFloatDef(LStrList[1], 0.0));
        LWangYin := Round(StrToFloatDef(LStrList[2], 0.0));
        if LJinE > 0 then
        begin
          if LDianHuaKa > 0 then
          begin
            LIndex := Length(LSqlAry);
            SetLength(LSqlAry, LIndex + 1);
            LSqlAry[LIndex].BiShu := LDianHuaKa;
            LSqlAry[LIndex].Amount := LJinE;
          end;
          if LWangYin > 0 then
          begin
            LIndex := Length(LSqlAry);
            SetLength(LSqlAry, LIndex + 1);
            LSqlAry[LIndex].BiShu := LWangYin;
            LSqlAry[LIndex].Amount := LJinE;
          end;
        end;
      end;
    end;

    while Length(LSqlAry) > 0 do
    begin
      LTotal := 0;
      for I := Low(LSqlAry) to High(LSqlAry) do
        Inc(LTotal, LSqlAry[I].BiShu);
      LIndex := Random(Length(LSqlAry));
      if Random(LTotal) < LSqlAry[LIndex].BiShu then
      begin
        LLine := Format('I', [
          LMonth, LSqlAry[LIndex].Amount, LSqlAry[LIndex].TradeStatus]);
        Writeln(LWrite, LLine);

        Dec(LSqlAry[LIndex].BiShu);
        if LSqlAry[LIndex].BiShu <= 0 then
        begin
          for I := LIndex to High(LSqlAry) - 1 do
          begin
            LSqlAry[I] := LSqlAry[I + 1];
          end;
          SetLength(LSqlAry, Length(LSqlAry) - 1);
        end;
      end;
    end;

    CloseFile(LRead);
    CloseFile(LWrite);
  end;

  LStrList.Free;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值