我有一个只有两个字段的表tab_1,字段为 city_code和帐单类别A01
其中city_code 是单值的比如北京为 001,天津为002,但是帐单类别是多值的。
city_code A01
001 100-103;130-133;180;185
002 120-122
002 138
003 111;113
004 569
等等;
我想要在另一个表tab_2中得到如下结果(即用A01字段名做字段值的头三位,将其原有值中的数据附加在其后作为tab_2 字段A01的值;100-103表示100至103),请大侠指点如何实现,非常感谢!
city_code A01
001 A01100
001 A01002
001 A01003
001 A01130
001 A01131
001 A01132
001 A01133
001 A01180
001 A01185
002 A01120
002 A01121
002 A01122
002 A0138
003 A01111
003 A01113
004 A01569
其中city_code 是单值的比如北京为 001,天津为002,但是帐单类别是多值的。
city_code A01
001 100-103;130-133;180;185
002 120-122
002 138
003 111;113
004 569
等等;
我想要在另一个表tab_2中得到如下结果(即用A01字段名做字段值的头三位,将其原有值中的数据附加在其后作为tab_2 字段A01的值;100-103表示100至103),请大侠指点如何实现,非常感谢!
city_code A01
001 A01100
001 A01002
001 A01003
001 A01130
001 A01131
001 A01132
001 A01133
001 A01180
001 A01185
002 A01120
002 A01121
002 A01122
002 A0138
003 A01111
003 A01113
004 A01569
type
PData = ^TData;
TData = record
sCityCode: string;
sA1Data: string;
end;
var
S, sSQL: string;
SLTemp1, SLTemp2: TStringList;
I, J, iMax, iMin: Integer;
P: PData;
SL: TList;
begin
//数据库的操作就不做了
//
SL := TList.Create;
SLTemp1 := TStringList.Create;
SLTemp2 := TStringList.Create;
with TAdoQuery.Create(nil) do
try
//取数据
//Connection :=
sSQL := 'SELECT city_code, A01 FROM tab_1';
Close;
SQL.Clear;
SQL.Add(sSQL);
while not Eof do
begin
S := FieldByName('A01').AsString;
S := StringReplace(S, ';', #13#10, [rfReplaceAll, rfIgnoreCase]);
SLTemp1.Text := S;
for I := 0 to SLTemp1.Count - 1 do
begin
//
S := SLTemp1[I];
S := StringReplace(S, '-', #13#10, [rfReplaceAll, rfIgnoreCase]);
SLTemp2.Text := S;
if SLTemp2.Count > 1 then
begin
iMin := StrToInt(SLTemp2[0]);
iMax := StrToInt(SLTemp2[1]);
for J := iMin to iMax do
begin
New(P);
P^.sCityCode := FieldByName('city_code').AsString;
P^.sA1Data := 'A01' + IntToStr(J);
SL.Add(P);
end;
end else
begin
New(P);
P^.sCityCode := FieldByName('city_code').AsString;
P^.sA1Data := 'A01' + SLTemp1[I];
SL.Add(P);
end;
end; Next;
end;
//写数据
for I := 0 to SL.Count - 1 do
begin
sSQL := 'INSERT INTO tab_2 (city_code, A01) VALUES ';
sSQL := sSQL + Format('(''%s'', ''%s'')',
[PData(SL[I])^.sCityCode, PData(SL[I])^.sA1Data]);
Close;
SQL.Clear;
SQL.Add(sSQL);
ExecSQL;
end;
finally
SLTemp1.Free;
SLTemp2.Free;
SL.Pack;
SL.Free;
Free;
end;
end;