procedure CopyRecord(Source, Destination: TDataSet);
var Ind:longint;
SField, DField: TField;
begin
for Ind:=0 to Source.FieldCount - 1 do
begin
SField := Source.Fields[ Ind ];
DField := Destination.FindField( SField.FieldName );
if (DField <> nil) and (DField.FieldKind = fkData) and
not DField.ReadOnly then
if (SField.DataType = ftString) or
(SField.DataType <> DField.DataType) then
DField.AsString := SField.AsString
else
DField.Assign( SField )
end;
end;
旧方法:
DEST.Open;
ORIGIN.Open;
while not ORIGIN.Eof do
begin
if ORIGINTYPE.AsString = 'T' then
with ORIGIN do
begin
DEST.Append;
DEST.FieldByName('TYPE').AsString := ORIGINTYPE.AsString;
DEST.FieldByName('FIRSTNAME').AsString := ORIGINFIRSTNAME.AsString;
DEST.FieldByName('LASTNAME').AsString := ORIGINLASTNAME.AsString;
DEST.FieldByName('CPF').AsString := ORIGINCPF.AsString;
DEST.FieldByName('PARTY').AsString := ORIGINPARTY.AsString;
DEST.Post;
end;
ORIGIN.Next;
end;
使用该过程的调用示范:
DEST.Open;
ORIGIN.Open;
while not ORIGIN.Eof do begin
if ORIGINTYPE.AsString = 'T' then begin
DEST.Append;
CopyRecord( ORIGIN, DEST );
DEST.Post;
end;
ORIGIN.Next;
end;
我在ORACLE中测试成功。
Create Table Test
(ID varchar2(5),
Name Varchar2(50)
);
insert into test values('10000','ABC');
insert into test select ID+1,Name from test where ID='10000';
你只要动态的改ID+1和后面的条件ID=10000就可以了。其它的数据库,只要支持SQL的都差不
多。
用Insert into ... Select ... SQL语句就搞定了。
先找到那个表中最大的主键值MaxKeyValue,然后用下面的SQL:
insert into TableName(ID, FieldList) select ID + MaxKeyValue, FieldList from TableName;