| Author |
Topic |
|
tvb2727
Starting Member
35 Posts |
Posted - 2010-10-19 : 21:00:18
|
| I need to make a trigger where I need to verify the updated purchase price is not less that what is already in the table. I tried to use an INSTEAD OF and was able to pull the value out of the table(before the update) and make the error happen, but if its correct and greater than what is already in the table it doesn't make the update. If you use a FOR then the updated is made before I can pull the earlier value out of the database to verify if its less than etc. Any ideas how to accomplish this? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tvb2727
Starting Member
35 Posts |
Posted - 2010-10-19 : 21:43:34
|
| Here is the code. Also, what if I have other triggers on the same table. Those will hit the update on this trigger and not work properly. Does that makes sense. I tried to do a FOR trigger that works fine, but it wouldn't even do a normal update because it was hitting the ELSE of the INSTEAD OF trigger. Please help if possible. I would be appreciated. Create TRIGGER price-trigON PricesInstead OF UPDATEASdeclare @pnum intdeclare @pprice1 decimal(10, 1)declare @pprice2 decimal(10, 1)select @pnum = inserted.PRICE_NUM from insertedselect @pprice1 = (select P_Price from pricemain where PRICE_NUM = @pnum)select @pprice2 = inserted.P_Price from insertedif (@pprice2 < @pprice1)begin rollback transaction Print 'Purchase Price can not be less than already in the Table!'end |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2010-10-20 : 00:42:52
|
multiple triggers per table. that's side-effect city! elsasoft.org |
 |
|
|
tvb2727
Starting Member
35 Posts |
Posted - 2010-10-20 : 15:14:40
|
| Answer to my issue:select @pprice1 = deleted.P_Price from deleted returns what was in there before the update went through with the FOR UPDATE statement. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tvb2727
Starting Member
35 Posts |
Posted - 2010-10-21 : 01:46:16
|
| For my case, it should be fine. Thanks for the information though for future references. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-10-21 : 07:11:06
|
"it should be fine"Scary assumption!At the very least do:IF SELECT COUNT(*) FROM deleted <> 1BEGIN raiserror(... ROLLBACK ...END Consider a manualUPDATE MyTableSET SomeColumn = ... some fixed value ...where you need to fix up the table en-masse. The trigger will fire ONCE to process all the records in one go. Chances are that you, or another DBA/DEV, will do that at some time ... |
 |
|
|
|