3
I want to create a Rigger that when firing calculates the customer’s age on each table row and updates itself.
I tried it this way:
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
Didn’t work.
What error or behaviour is shown?
– Luis Henrique
Does not perform the update.
– Anderson
You’re not telling me where to update. Ta missing clause
where
in theupdate
, nay?– mutlei
@really mutlei this was missing. but I changed and continues the same way. Thanks !
– Anderson
@Anderson Try to run the script without being on
trigger
and confirm that it is working. It may not be entering theif
ofupdate
.– Jothaz
@Jota I did this... I really don’t think it’s entering if. Because SQLSERVER returns:"Command(s) completed successfully." But nothing happens....
– Anderson