有两个办法:
办法1: 把这个查询做成视图
办法2:每个表都用一个UpdateSQL
李维的Delphi5.x多层应用系统篇第三章有详细介绍,建议找来看看
我把部分代码贴出来:
DataSetProvider的BeforeUpdateRecord事件
procedure TMultiTablesUpdateServer.dspMultiJoinsBeforeUpdateRecord(
Sender: TObject; SourceDS: TDataSet; DeltaDS: TClientDataSet;
UpdateKind: TUpdateKind; var Applied: Boolean);
begin
quryMultiJoin.UpdateObject := usCustomers;
SetParams(usCustomers, DeltaDS, UpdateKind);
usCustomers.ExecSQL(UpdateKind);
quryMultiJoin.UpdateObject := usOrders;
SetParams(usOrders, DeltaDS, UpdateKind);
usOrders.Apply(UpdateKind);
quryMultiJoin.UpdateObject := usEmployee;
SetParams(usEmployee, DeltaDS, UpdateKind);
usEmployee.Apply(UpdateKind);
Applied := True;
end;
//SetParms的代码
procedure TMultiTablesUpdateServer.SetParams(FUpdateSQL : TUpdateSQL; DeltaDS: TClientDataSet; UpdateKind: TUpdateKind);
var
I: Integer;
Old: Boolean;
Param: TParam;
PName: string;
Field: TField;
Value: Variant;
begin
if not Assigned(FUpdateSQL.DataSet) then
Exit;
with FUpdateSQL.Query[UpdateKind] do
begin
for I := 0 to Params.Count - 1 do
begin
Param := Params;
PName := Param.Name;
Old := CompareText(Copy(PName, 1, 4), 'OLD_') = 0;
if Old then
System.Delete(PName, 1, 4);
Field := DeltaDS.FindField(PName);
if not Assigned(Field) then
Continue;
if Old then
Param.AssignFieldValue(Field, Field.OldValue)
else
begin
Value := Field.NewValue;
if VarIsEmpty(Value) then
Value := Field.OldValue;
Param.AssignFieldValue(Field, Value);
end;
end;
end;
end;