Henri
Henri

Reputation: 5113

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)

Upvotes: 3

Views: 7573

Answers (1)

anon
anon

Reputation:

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

Upvotes: 8

Related Questions