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.
| Author |
Topic |
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2011-09-21 : 02:53:18
|
| Hi all,I have a table "Products" like this one:ProductID - Price - OtherColumnwith some values23 - 34.9924 - 15.9012 - 120.005 - 34.0032 - 35.90.....How can I write a trigger that updates automatically this table when someone or something make an Insert or Update to set the Price field = 0 when its value is negative, and this *only* for ProductID = 32?Thanks in advance.Luigi |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-21 : 05:43:22
|
| [code]CREATE TRIGGER UpdatePriceON ProductsFOR INSERT,UPDATE ASBEGINUPDATE tSET t.Price=0FROM Products tINNER JOIN INSERTED iON i.ProductID = t.ProductID AND i.ProductID =32WHERE i.Price < 0END[/code]I would have done this by means of check constraint though to avoid this insert/update first off all.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Ciupaz
Posting Yak Master
232 Posts |
Posted - 2011-09-21 : 08:03:39
|
| Perfect, thank you Visakh.Luigi |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-21 : 08:47:27
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|