# Trigger: iPurchaseOrderDetail

## Properties

Property | Value
--- | ---
Trigger | iPurchaseOrderDetail
ID | 1611152785
Owner | dbo
Schema | Purchasing
For Object | PurchaseOrderDetail
Disabled |
Is Not For Replication |
Trigger Type Description | SQL TRIGGER
ANSI NULLS |
QUOTED IDENTIFIER |
Encrypted |
MS Shipped |
Execute As | Caller
Modify Date | 8/8/2010 5:23:44 PM
Creation Date | 8/8/2010 5:23:44 PM

## Extended Properties

Name | Value
--- | ---
MS_Description | AFTER INSERT trigger that inserts a row in the TransactionHistory table and updates the PurchaseOrderHeader.SubTotal column.

## Trigger Type

Is After Trigger | Is Instead Of | Insert Trigger | Update Trigger | Delete Trigger
--- | --- | --- | --- | ---

## First / Last Trigger

First Delete Trigger | First Insert Trigger | First Update Trigger | Last Delete Trigger | Last Insert Trigger | Last Update Trigger
--- | --- | --- | --- | --- | ---

## Objects that depend on [iPurchaseOrderDetail]

- iPurchaseOrderDetail

## Objects on which [iPurchaseOrderDetail] depends

- iPurchaseOrderDetail
  - Purchasing.PurchaseOrderDetail
    - Production.TransactionHistory
      - Production.Product
        - Production.ProductSubcategory
          - Production.ProductCategory
            - dbo.Name
        - Production.UnitMeasure
        - Production.ProductModel
          - Production.ProductDescriptionSchemaCollection
          - Production.ManuInstructionsSchemaCollection
        - dbo.Flag
      - datePS
        - dataPF1
    - Purchasing.PurchaseOrderHeader
      - HumanResources.Employee
        - Person.Person
          - Person.AdditionalContactInfoSchemaCollection
          - Person.BusinessEntity
          - Demographics.exist
          - Person.IndividualSurveySchemaCollection
          - dbo.NameStyle
      - Purchasing.Vendor
        - dbo.uspLogError
          - dbo.ErrorLog
          - dbo.uspPrintError
        - dbo.AccountNumber
      - Purchasing.ShipMethod

## SQL

Script
SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON CREATE TRIGGER [Purchasing].[iPurchaseOrderDetail] ON [Purchasing].[PurchaseOrderDetail] AFTER INSERT AS BEGIN DECLARE @Count int; SET @Count = @@ROWCOUNT; IF @Count = 0 RETURN; SET NOCOUNT ON; BEGIN TRY INSERT INTO [Production].[TransactionHistory] ([ProductID] ,[ReferenceOrderID] ,[ReferenceOrderLineID] ,[TransactionType] ,[TransactionDate] ,[Quantity] ,[ActualCost]) SELECT inserted.[ProductID] ,inserted.[PurchaseOrderID] ,inserted.[PurchaseOrderDetailID] ,'P' ,GETDATE() ,inserted.[OrderQty] ,inserted.[UnitPrice] FROM inserted INNER JOIN [Purchasing].[PurchaseOrderHeader] ON inserted.[PurchaseOrderID] = [Purchasing].[PurchaseOrderHeader].[PurchaseOrderID]; UPDATE [Purchasing].[PurchaseOrderHeader] SET [Purchasing].[PurchaseOrderHeader].[SubTotal] = (SELECT SUM([Purchasing].[PurchaseOrderDetail].[LineTotal]) FROM [Purchasing].[PurchaseOrderDetail] WHERE [Purchasing].[PurchaseOrderHeader].[PurchaseOrderID] = [Purchasing].[PurchaseOrderDetail].[PurchaseOrderID]) WHERE [Purchasing].[PurchaseOrderHeader].[PurchaseOrderID] IN (SELECT inserted.[PurchaseOrderID] FROM inserted); END TRY BEGIN CATCH EXECUTE [dbo].[uspPrintError]; IF @@TRANCOUNT > 0 BEGIN ROLLBACK TRANSACTION; END EXECUTE [dbo].[uspLogError]; END CATCH; END; EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'AFTER INSERT trigger that inserts a row in the TransactionHistory table and updates the PurchaseOrderHeader.SubTotal column.' , @level0type=N'SCHEMA',@level0name=N'Purchasing', @level1type=N'TABLE',@level1name=N'PurchaseOrderDetail', @level2type=N'TRIGGER',@level2name=N'iPurchaseOrderDetail'

#### See also

List of Triggers