title: Make column to always contain calculated value without triggers
body: <p>I was doing practice tests before 70-461 exam and one of questions was to create table that:</p>
  
  <ul>
  <li>Has 4 columns, saleid, unitprice, amount, and price.</li>
  <li>Price is calculated by multiplication of unitprice and amount</li>
  <li>You can't use triggers.</li>
  </ul>
  
  <p>The last one killed me. How can I do it without triggers? Here is my solutions using instead of triggers.</p>
  
  <pre><code>CREATE TABLE [HR].[Stuff](
      [saleid] [int] IDENTITY(1,1) NOT NULL,
      [unitprice] [float] NOT NULL,
      [amount] [float] NOT NULL,
      [price] [float] NULL,
  )
  GO
  
  CREATE TRIGGER [calculate_price_insert]
  ON [HR].[Stuff]
  INSTEAD OF INSERT
  AS
      DECLARE @unitprice float = (SELECT TOP 1 unitprice from inserted )
      DECLARE @amount float = (SELECT TOP 1 amount from inserted)
      INSERT INTO HR.[Stuff]
      VALUES(@unitprice,@amount, @unitprice*@amount)
  GO
  
  CREATE TRIGGER [calculate_price_update]
  ON [HR].[Stuff]
  INSTEAD OF UPDATE
  AS
      DECLARE @unitprice float = (SELECT TOP 1 unitprice from inserted )
      DECLARE @amount float = (SELECT TOP 1 amount from inserted)
      UPDATE HR.[Stuff]
      SET unitprice = @unitprice, amount = @amount, price = @unitprice*@amount
      WHERE unitprice = (SELECT TOP 1 saleid from inserted)
  GO
  </code></pre>