I want to create a trigger that, when triggered, calculates the age of the client in each row of the table and updates if necessary.
I tried the following:
create trigger Atualiza_Idade
on cliente
for insert,update,delete
as
declare
@i int,
@dtnasc datetime ,
@Idade int,
@Hoje DATETIME,
@Condicao int
Set @i=1
Set @Hoje=(SELECT GETDATE ( ))
while (@i<(select COUNT( *) from Cliente ))
begin
select @dtnasc=DataNasimento,@Idade=Idade from Cliente where id=@i;
set @Condicao = (SELECT FLOOR(DATEDIFF(DAY, @dtnasc, @Hoje) / 365.25));
if (@Condicao<>(select idade from Cliente where id=@i))
update Cliente set Idade=@Condicao where id=@i;
set @i=@i+1;
end
go
It did not work.