Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Making a trigger check result

Author  Topic 

skelle
Starting Member

1 Post

Posted - 2012-05-30 : 14:54:01
Hello,

I want to make a trigger to check a result. That result may not be higher or equal to 20. When it is higher than 20, it has to be replaced by 20.

Someone out there who can help me?

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-05-30 : 15:07:42
It's better to have a CHECK constraint on the table that prevents values higher than 20 from being inserted or updated. Is this data being imported from a different source, or being entered by hand? If it's being imported then you should have an ETL process that changes higher values to 20, rather than having a trigger do it.

While a trigger can certainly do this, you could encounter performance issues, and it can't guarantee the data will be correct.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-06-04 : 09:30:15
Do you have data already in the table? You can just use a SELECT statement

select case when col>20 then 20 else col end as col from table

If you want to restrict the values while adding to table, better option is as suggested, to implement a check constraint

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -