title: Creating a trigger to insert records from a table to another one. Triggered column of table 'A' is inserted as null in table 'B'
body: <p>I have two tables <code>tbl_PurchaseDetails</code> and <code>tbl_ItemDetails</code>. I need to insert some records into <code>tbl_ItemDetails</code> from <code>tbl_PurchaseDetails</code>, right after it is inserted in <code>tbl_PurchaseDetails</code>. <code>tbl_PurchaseDetails</code> has auto generated custom field <code>PurchaseID</code>.</p>
  
  <p><strong>Code for auto generation of <code>PurchaseID</code> is:-</strong>
  *<em>This trigger works perfectly</em>*</p>
  
  <pre><code>CREATE FUNCTION CreatePurchaseID (@id INT) 
  RETURNSvarchar(10)
  AS
  BEGIN
  RETURN 'P' + CONVERT(VARCHAR(10), @id)
  END
  
  CREATE TRIGGER trigger_PurchaseID ON tbl_PurchaseDetails 
  FOR INSERT AS
  UPDATE
      tbl_PurchaseDetails 
  SET
      tbl_PurchaseDetails.PurchaseID = dbo.CreatePurchaseID(tbl_PurchaseDetails.ID) 
  FROM
      tbl_PurchaseDetails 
  INNER JOIN 
  INSERTED on tbl_PurchaseDetails.ID= INSERTED.ID
  </code></pre>
  
  <p><strong>I have written the following code for trigger to insert into <code>tbl_ItemDetails</code>:-</strong></p>
  
  <pre><code>CREATE TRIGGER trigger_UpdateItemDetails ON tbl_PurchaseDetails
  FOR INSERT AS
  DECLARE @PurchaseID VARCHAR(20)
  DECLARE @Quantity INT
  DECLARE @WarehouseID VARCHAR(20)
  
  SELECT @PurchaseID=(PurchaseID) FROM INSERTED
  SELECT @Quantity=(ItemQuantity) FROM INSERTED
  SELECT @WarehouseID=(WarehouseID) FROM INSERTED
  
  INSERT INTO 
  tbl_ItemDetails
  (PurchaseID,Quantity,WarehouseID)
  VALUES
  (
  @PurchaseID,@Quantity,@WarehouseID
  )
  </code></pre>
  
  <p>**And now when i insert into <code>tbl_PurchaseDetails</code> the records are added to <code>tbl_PurchaseDetails</code> and <code>tbl_ItemDetails</code> successfully. The problem here is, the <code>PurchaseID</code> is inserted as null in <code>tbl_ItemDetails</code>. It is inserted as expected in <code>tbl_PurchaseDetails</code> though.</p>