Question

I have a table called person, one of the attributes is years_worked.

I need to find a way to restrict years_worked from being decreased, so that a trigger/assertion (not sure what to use here) will only allow increases on updates.

Was it helpful?

Solution

The below code will just revert all years_worked updated values that don't fall into your restriction back to what they were before the update. This is for SQL Server, I can't speak for other RDBMS'.

create trigger RestrictYearsWorked
on person
after update
as

    update person
    set years_worked = d.years_worked
    from person p
    inner join deleted d
    on p.yourIdCol = d.yourIdCol
    where p.years_worked < d.years_worked

go
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top