Properties

Owner: Sales 
Table/View: Sales.Store 
Disabled:  
Encrypted:  
Creation Date: 04/26/2006 
Modification Date: 04/26/2006 
Description: AFTER INSERT trigger inserting Store only if the Customer does not exist in the Individual table. 

Creation options

QUOTED_IDENTIFIER:  
ANSI_NULLS:  

Type

Instead of:  
Insert:  
Update:  
Delete:  

Objects that [Sales].[iStore] depends on

Object Name Owner Object Type Dep Level
  Flag  dbo  User Defined type 
  Name  dbo  User Defined type 
  NameStyle  dbo  User Defined type 
  Phone  dbo  User Defined type 
  ufnLeadingZeros  dbo  Function 
  Contact  Person  Table 
  ErrorLog  dbo  Table 
  SalesTerritory  Sales  Table 
  uspPrintError  dbo  Procedure 
  Customer  Sales  Table 
  Employee  HumanResources  Table 
  uspLogError  dbo  Procedure 
  Individual  Sales  Table 
  SalesPerson  Sales  Table 
  Store  Sales  Table 
Total: 15 object(s)

SQL

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE TRIGGER [Sales].[iStore] ON [Sales].[Store]
AFTER INSERT AS
BEGIN
    DECLARE @Count int;

    SET @Count = @@ROWCOUNT;
    IF @Count = 0
        RETURN;

    SET NOCOUNT ON;

    BEGIN TRY
        -- Only allow the Customer to be a Store OR Individual
        IF EXISTS (SELECT * FROM inserted INNER JOIN [Sales].[Individual]
            ON inserted.[CustomerID] = [Sales].[Individual].[CustomerID])
        BEGIN
            -- Rollback any active or uncommittable transactions
            IF @@TRANCOUNT > 0
            BEGIN
                ROLLBACK TRANSACTION;
            END
        END;
    END TRY
    BEGIN CATCH
        EXECUTE [dbo].[uspPrintError];

        -- Rollback any active or uncommittable transactions before
        -- inserting information in the ErrorLog
        IF @@TRANCOUNT > 0
        BEGIN
            ROLLBACK TRANSACTION;
        END

        EXECUTE [dbo].[uspLogError];
    END CATCH;
END;

GO
SET QUOTED_IDENTIFIER OFF
GO

GO
SET ANSI_NULLS OFF
GO

See Also

List of triggers