Subject More complex syntax for trigger
Author diegodelafuente
Yesterday I asked for a problem with a trigger created to track changes in any field of a table.
In this query I must write every field manually.
I like to improve this query to do it automatically.

I think if I can list the fields of the table using a cursor, perhaps after that I will able to check every field in that cursor to track the changes

With this sentence I can list all the fields of the table "CONTRATOS" using a cursor.

execute block
returns (FieldName char(30))
as
declare cur cursor for
(Select f.rdb$field_name
from rdb$relation_fields f
join rdb$relations r on f.rdb$relation_name = r.rdb$relation_name
and r.rdb$view_blr is null
and (r.rdb$system_flag is null or r.rdb$system_flag = 0)
Where f.rdb$relation_name = 'CONTRATOS'
order by 1, f.rdb$field_position);
begin
open cur;
while (1=1) do
begin
fetch cur into FieldName;
if (row_count = 0) then leave;
suspend;
end
close cur;
end


This is my trigger. I must add all the fields manually.
AS
declare variable Cadena varchar(500);
begin
if (old.idnomencladoraux IS DISTINCT FROM new.idnomencladoraux) then
if (:cadena is null) then
cadena = 'NomAux: ' || old.idnomencladoraux || ', por: ' || new.idnomencladoraux;
else
cadena = :cadena || '\n NomAux: ' || old.idnomencladoraux || ', por: ' || new.idnomencladoraux;

if (old.descripcionaux IS DISTINCT FROM new.descripcionaux) then
if (:cadena is null) then
cadena = 'DescAux: ' || old.descripcionaux || ', por: ' || new.descripcionaux;
else
cadena = :cadena || '\n DescAux: ' || old.descripcionaux || ', por: ' || new.descripcionaux;

/* I must add all the fields manually */


if (:cadena is null) then cadena = 'Sin Cambios';
insert into contratoshist (cadena) Values (:cadena);

end

Rgds
Diego