Question

I am using absolute database as my database. Since absolute database does not support triggers I have to write my own UPDATE trigger. I tried this way :

procedure TDataModule2.ABSQuery6AfterPost(DataSet: TDataSet);
begin
  ABSQuery4.Close;
  ABSQuery4.SQL.Clear;
  ABSQuery4.SQL.Text :='UPDATE MYTABLE SET RECORDCHANGED=CURRENT_TIMESTAMP';
  ABSQuery4.ExecSQL;
end;

But this way I am updating the entire table. How can I update only the record (row) that was changed ?

edit : This is how I made it work

procedure TDataModule2.ABSQuery6AfterPost(DataSet: TDataSet);
begin
with ABSQuery4 do begin
ABSQuery4.Close;
ABSQuery4.SQL.Clear;
ABSQuery4.SQL.Text :='UPDATE MYTABLE SET RECORDCHANGED=CURRENT_TIMESTAMP WHERE    T_ID=:a1';
ABSQuery4.Params.ParamByName('a1').AsInteger := ABSQuery6.FieldByName('T_ID').AsInteger;
ABSQuery4.ExecSQL;
end;
end;
Was it helpful?

Solution 2

You need to add to your query a 'where' clause. The SQL becomes

update mytable
set recordchanged = current_timestamp
where id = id_of_last_changed_record

It's best to pass the id of the last changed record via a parameter, making your code

with ABSQuery4 do
 begin
  Close;
  SQL.Clear;
  SQL.Text :='UPDATE MYTABLE SET RECORDCHANGED=CURRENT_TIMESTAMP WHERE ID = :P1';
  sql.params[0].asinteger:= id_of_last_changed_record;
  ExecSQL;
 end;

OTHER TIPS

This can be solved very straight forward with the TDataSet.BeforePost event (also have a look at the code sample from docwiki)

procedure TDataModule2.ABSQuery6BeforePost(DataSet: TDataSet);
begin
  DataSet['RECORDCHANGED'] := now;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top