Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference updated row for SQL Server trigger

I want to always update the value of an update row in the database.

Imagine, i have a table with names and prices Every time a row is inserted or updated, i want to lower the price by a fixed amount.

How can I do this with SQL server 2005?

I have now something like

CREATE TRIGGER LowerPriceOnInsert ON products
AFTER INSERT, UPDATE
AS
    IF UPDATE(ProductPrice)
like image 999
Henri Avatar asked Jul 15 '26 08:07

Henri


1 Answers

Ok, so let's say you wanted to reduce the price by 5 cents:

UPDATE p
  SET price = price - 0.05
  FROM dbo.Products AS p
  INNER JOIN inserted AS i
  ON p.ProductID = i.ProductID;

http://msdn.microsoft.com/en-us/library/ms191300.aspx

like image 134
Aaron Bertrand Avatar answered Jul 17 '26 17:07

Aaron Bertrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!