Trigger to update date modified date in firebird

1

I have a client table with the Registry Data Date and Registry Modification Date fields would anyone know how to create a trigger that updates the Modified Date column of this customer table every time a row is changed, so far I've thought of something like this here:

CREATE trigger TBCLIENTE_DATAMODIFICACAO for TB_CLIENTE
active before update position 0
AS
begin
  update TB_CLIENTE set TB_CLIENTE.DATA_MODIFICACAO = current_date where TB_CLIENTE.CODIGO = ????;
end

Where ???? is a parameter that I can not put correctly.

    
asked by anonymous 02.09.2016 / 14:22

3 answers

1

Well, thanks to the tip of my friend Anthony Accioly, I managed to solve my problem, I believe that my strategy worked well with the following code:

CREATE trigger TBCLIENTE_DATAMODIFICACAO for TB_CLIENTE
active before update position 0
AS
begin
  if (new.DATA_MODIFICACAO = CURRENT_DATE) then
    begin
      /*NÃO FAZ NADA PORQUE A DATA FOI ATUALIZADA*/
    end
  else
    begin
      update TB_CLIENTE set TB_CLIENTE.DATA_MODIFICACAO = CURRENT_DATE where TB_CLIENTE.CODIGO = new.CODIGO;
    end
end

IF checks to see if the Modification Date in the registry update equals the current date, if the expression is true then the record has already been modified today , then it ignores the statement otherwise it updates the modification date for the current date     

04.09.2016 / 22:56
2

According to Julio's statement, running a update statement in trigger is not a very good idea. If this were allowed in your case you would enter loop trying to update the date of the last modification. That said, you can always use a syntax by modifying the value of the new variable in trigger :

CREATE trigger TBCLIENTE_DATAMODIFICACAO for TB_CLIENTE
active before update position 0
AS
begin
  if (new.DATA_MODIFICACAO is null)
    then new.DATA_MODIFICACAO = current_date;
end

Notice that I used if to check that DATA_MODIFICACAO is not populated. Using this if you can enter a modification date manually if you wish. If this is not desirable (i.e., you want the DATA_MODIFICACAO to be replaced with current_date ) simply remove if .

Source: The Firebird FAQ - How to get the timestamp of last record change?

    
02.09.2016 / 16:28
-1

You can not do this because sgbd itself will prevent it. If you could, it would create an infinite loop!

    
02.09.2016 / 14:36